Cant get the proper use for DropDownListFor with a model and a viewbag element
- by EH_warch
I have a list of locations set in the ViewBag element like this:
public ActionResult Create()
{
var db = new ErrorReportingSystemContext();
IEnumerable<SelectListItem> items = db.Locations
.AsEnumerable()
.Select(c => new SelectListItem
{
Value =c.id.ToString(),
Text = c.location_name
});
ViewBag.locations = items;
return View();
}
I'm trying to get the values from ViewBag.locations from the view like this
@Html.DropDownListFor(model => model.location_fk_id, ViewBag.locations);
//@Html.DropDownListFor(model => model.location_fk_id, @ViewBag.locations);
//@Html.DropDownListFor(model => model.location_fk_id, "locations");
But no avail. How can i use it?