heloo guys Viewbag 객체를 사용하여 mvc의 데이터베이스에서 dropdownlist를 채 웁니다. 이제 Select Supplier와 같은 기본값을 지정하고 싶습니다. 여기 내 제어 코드입니다. . 지정하는 방법 드롭 다운의 기본값은 MVC에서 Viewbag를 사용하여 데이터베이스에서 채워졌습니다
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);
}
내 ViewCode 당신이 ViewBag 및
DropDownList
HTML 도우미 방법을 사용하는 경우
<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>
Shyju와 JamieD77이 너무 감사합니다. Jamie Soloution을 다시 사용해 주셔서 감사합니다. – Imran