ASP.Net MVC脚手架中的级联下拉列表

时间:2015-01-08 05:43:13

标签: c# asp.net-mvc asp.net-mvc-scaffolding

是否可以在ASP.Net MVC脚手架中使用级联下拉列表?如果是,怎么样?

我这里有一些代码会显示使用脚手架的下拉列表:

MyController.cs

// GET:/ ManualEntries / Create

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>

1 个答案:

答案 0 :(得分:0)

你可以这样做

<强>控制器

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)