1

我在ViewBag元素中设置了一个位置列表,如下所示:

    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();
    } 

我试图从这样的视图中获取 ViewBag.locations 的值

    @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");

但无济于事。我该如何使用它?

4

1 回答 1

3

如果你这样做,你只需要投射它:

@Html.DropDownListFor(model => model.location_fk_id, (IEnumerable<SelectListItem>)ViewBag.locations)
于 2012-09-17T21:39:53.207 回答