I can create a dropdown list with items from the ViewBag, but there doesn’t seem to be a way to select the current item. I’m not using the model to achieve this, just want to use ViewBag, otherwise I’d have to create another model class just to wrap the select items and the actual data to display.
My code:
@Html.DropDownList("MyFilter", (IEnumerable<SelectListItem>)ViewBag.SelectItems, "Show All" )
There is no parameter to set the value of the select list. Am I overlooking something?
If I were to use DropDownListFor<> does this only work against the model?
You could try setting the
MyFilterproperty in yourViewBag/ViewData:But my recommendation is to use view models and the strongly typed version of the helper:
and in your strongly typed view: