2011-03-17 72 views
2

如何在MVC 3中将ViewBag中的对象转换为IEnumerable?剃刀 - 在下拉列表中投射IEnumerable

我想这样做soethnig这样的:

@Html.DropDownListFor(model => model.CategoryID, @(IEnumerable<SelectListItem>)ViewBag.CategoriesList) 

的下拉列表采用了IEnumerable作为参数,但我不知道如何正确地投放。上面的代码返回一个错误。

回答

3
@Html.DropDownListFor(
    model => model.CategoryID, 
    (IEnumerable<SelectListItem>)ViewBag.CategoriesList 
) 
0
@Html.DropDownList("categoryId", 
      new SelectList(ViewBag.Categories as System.Collections.IEnumerable, 
      "categoryId", "categoryName", Model.categoryId))