开发者

Razor - cast IEnumerable in dropdownlist

开发者 https://www.devze.com 2023-02-18 01:00 出处:网络
How to cast in MVC 3 an object from ViewBag to IEnumerable ? i would like to do soethnig like that: @Html.DropDownListFor(model => model.CategoryID, @(IEnumerable<SelectListItem>)ViewBag.Ca

How to cast in MVC 3 an object from ViewBag to IEnumerable ?

i would like to do soethnig like that:

@Html.DropDownListFor(model => model.CategoryID, @(IEnumerable<SelectListItem>)ViewBag.CategoriesList)
开发者_如何学C

the dropdownlist takes IEnumerable as an argument, but I don't know how to cast it properly. The above code returns an error.


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


@Html.DropDownList("categoryId",
            new SelectList(ViewBag.Categories as System.Collections.IEnumerable,
            "categoryId", "categoryName", Model.categoryId))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号