heloo Jungs Ich habe Dropdown-Liste aus der Datenbank in mvc mit Viewbag Objekt bevölkert jetzt möchte ich Standard-Wert für wie Select Lieferant hier ist meine Controler-Code. .wie man den Standardwert für Dropdown-Liste ich habe von der Datenbank mit Viewbag in mvc
public ActionResult Create()
{
//IEnumerable<SelectListItem> SuppliersList = db.Suppliers.Select(c => new SelectListItem
//{
// Value = c.Name,
// Text = c.Name
//});
ViewBag.SupplierList = new SelectList(db.Suppliers, "Name", "Name");
return PartialView("_Create");
}
// POST: /Frames/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include="FrameId,Name,FrameCode,FrameFor,Brand,Material,Color,FrameType,FrameShape,Bridge,LensWidth,FrameWidth,TempleLength,LensHeight,FrameWeight,Quantity,PurchaseRate,SaleRate,Supplier,Discription,Status,CreatedOn")] Frame frame)
{
if (ModelState.IsValid)
{
db.Frames.Add(frame);
await db.SaveChangesAsync();
return Json(new { success = true });
}
return PartialView("_Create", frame);
}
und meine Viewcode
<div class="col-md-3">
@Html.LabelFor(model => model.Supplier)
@Html.DropDownList("Supplier", ViewBag.SupplierList as SelectList, new { @class = "form-control"})
@Html.ValidationMessageFor(model => model.Supplier)
</div>
Vielen Dank Shyju und JamieD77 Ich habe versucht, die Jamie Soloution es funktioniert Danke nochmal. – Imran