我的选择列表没有选择默认对象被带到通过代码。
我第一次创建我SelectList
像这样:
public SelectList CreateSelectList(object objSelected = null)
{
return new SelectList(GetAll().OrderBy(s => s.NumericalValue), "PeriodID", "Name", objSelected);
}
我objSelected被充满了与该PeriodID关联的GUID。
在我的控制,我定义我的viewbag
变量,新的选择列表。
public ActionResult Edit(Guid id)
{
Classroom classroom = classroomRepository.GetByID(id);
ViewBag.PeriodID = periodRepository.CreateSelectList(classroom.PeriodID);
return View(classroom);
}
然后,在我看来这里就是我如何显示我SelectList
:
<div class="control-group">
@Html.LabelFor(model => model.PeriodID, "Period", new { @class = "control-label" })
<div class="controls">
@Html.DropDownListFor(model => model.PeriodID, ViewBag.PeriodID as SelectList, String.Empty, new { @class = "span3" })
@Html.ValidationMessageFor(model => model.PeriodID)
</div>
</div>