我有一个控制器,它给一个视图SelectList
,然后为SelectList
呈现多个DropDownLists
。我现在想让DropDownLists
具有不同的值以供默认选择。有没有办法做到这一点?从SelectList为DropDownLists设置不同的默认值
编辑:哦,当然,我想要默认的值可以从我的模型中获得。 Model.Dj1_Id
等
控制器:
[HttpGet]
public ActionResult EditPartyInfo(int ID)
{
Party prty = db.Partys.Find(ID);
ViewBag.People = new SelectList(db.People, "Id", "Name");
return View(prty);
}
查看:
@model Musa.Models.Party
@using (Html.BeginForm("EditPartyInfo", "Events", FormMethod.Post))
{
@Html.AntiForgeryToken()
<!-- Some text input fields -->
<div class="form-group">
<label for="Dj1_Id">Dj 1</label>
<div class="form-inline">
@Html.DropDownList("Dj1_Id", ViewBag.People as SelectList, String.Empty, new { @class = "form-control person-select", style = "width: 50%;" })
</div>
</div>
<div class="form-group">
<label for="Dj2_Id">Dj 2</label>
<div class="form-inline">
@Html.DropDownList("Dj2_Id", ViewBag.People as SelectList, String.Empty, new { @class = "form-control person-select", style = "width: 50%;" })
</div>
</div>
<div class="form-group">
<label for="Dj3_Id">Dj 3</label>
<div class="form-inline">
@Html.DropDownList("Dj3_Id", ViewBag.People as SelectList, String.Empty, new { @class = "form-control person-select", style = "width: 50%;" })
</div>
</div>
<input type="submit" class="btn btn-primary" value="Los geht's" />
}
我竟然落得这样做:
控制器:
[HttpGet]
public ActionResult EditPartyInfo(int ID)
{
Party prty = db.Partys.Find(ID);
ViewBag.People = db.People;
return View(prty);
}
查看:
/*...*/
@Html.DropDownList("Dj1_Id", new SelectList(ViewBag.People, "Id", "Name", Model.Dj1_Id), String.Empty, new { @class = "form-control person-select", style = "width: 50%;" })
/*...*/
@Html.DropDownList("Dj2_Id", new SelectList(ViewBag.People, "Id", "Name", Model.Dj2_Id), String.Empty, new { @class = "form-control person-select", style = "width: 50%;" })
/*...*/
@Html.DropDownList("Dj3_Id", new SelectList(ViewBag.People, "Id", "Name", Model.Dj3_Id), String.Empty, new { @class = "form-control person-select", style = "width: 50%;" })