MVC 基架中的级联下拉列表 ASP.Net
本文关键字:下拉列表 ASP Net 级联 MVC | 更新日期: 2023-09-27 18:36:37
是否可以在 ASP.Net MVC 脚手架中使用级联下拉列表?如果是,如何?
我这里有一些代码,这些代码将使用脚手架显示下拉列表:
我的控制器.cs
获取:/手动条目/创建
public ActionResult Create()
{
ViewBag.cluscd = new SelectList(db.CLUSTERs, "clus_id", "clusdesc");
ViewBag.seccd = new SelectList(db.SECTORs, "sec_id", "secdesc");
return View();
}
Create.cshtml
<div class="form-group">
@Html.LabelFor(model => model.sec_id, "Sector", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("sec_id", String.Empty)
@Html.ValidationMessageFor(model => model.sec_id)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.clus_id, "Cluster", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("clus_id", String.Empty)
@Html.ValidationMessageFor(model => model.clus_id)
</div>
</div>
你可以这样做
控制器
List<SelectListItem> selectlist = new List<SelectListItem>();
//fill the list and assign to viewbag and than access in view
ViewBag.SelectList = selectlist;
视图
@Html.DropDownList("name",(IEnumerable<SelectListItem>)ViewBag.SelectList)