Cant get the proper use for DropDownListFor with a model and a viewbag element

Posted by EH_warch on Stack Overflow See other posts from Stack Overflow or by EH_warch
Published on 2012-09-17T21:36:34Z Indexed on 2012/09/17 21:37 UTC
Read the original article Hit count: 200

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-3

Related posts about Razor