Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor - cast IEnumerable in dropdownlist

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)

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

like image 390
Sarzniak Avatar asked Nov 26 '25 21:11

Sarzniak


1 Answers

@Html.DropDownListFor(
    model => model.CategoryID, 
    (IEnumerable<SelectListItem>)ViewBag.CategoriesList
)
like image 162
Darin Dimitrov Avatar answered Nov 28 '25 09:11

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!