小编Joh*_*ina的帖子

为什么这会呈现为"System.Web.Mvc.SelectListItem"的列表?

我正在尝试使用从属性中提取的值来填充DropDownList,而我现在的最终结果是除了"System.Web.Mvc.SelectListItem"之外的任何内容.我敢肯定,我在这里省略了一些小步骤,但对于我的生活,我无法弄清楚它是什么.

属性GET生成列表:

public IEnumerable<SelectListItem> AllFoo {
    get {
        var foo = from g in Bar
                  orderby g.name
                  select new SelectListItem {
                     Value = g.fooid.ToString(),
                     Text = g.name
                  };

        return foo.AsEnumerable();
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器代码:

public ActionResult Edit(string id) {
    // n/a code
    ViewData["fooList"] = new SelectList(g.AllFoo, g.fooid);

    return View(g);
}
Run Code Online (Sandbox Code Playgroud)

视图代码:

<%= Html.DropDownListFor(model => model.fooid, ViewData["fooList"] as SelectList) %>
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc linq-to-sql

15
推荐指数
2
解决办法
1万
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

linq-to-sql ×1