My select list isn't selecting the default object being brought in through code.
I first create my SelectList
like so:
public SelectList CreateSelectList(object objSelected = null)
{
return new SelectList(GetAll().OrderBy(s => s.NumericalValue), "PeriodID", "Name", objSelected);
}
My objSelected gets filled with a Guid that's associated with the PeriodID.
Inside my controller I define my viewbag
variable to the new select list.
public ActionResult Edit(Guid id)
{
Classroom classroom = classroomRepository.GetByID(id);
ViewBag.PeriodID = periodRepository.CreateSelectList(classroom.PeriodID);
return View(classroom);
}
Then in my View here's how I'm displaying my 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>