asp mvc DropDownList how to pass a selected item in visual basic .net
i put this in the view:
<%=Html.DropDownList("Estados", ViewData("EstadosList"))%>
and the compiler says
that i have to convert to SelectList becouse ViewData Returns an Object
any idea to do this?
thxs..
Fernando Soruco
Best Answer
There are two ways to solve your problem. One is faster, and one is better...
Better version:
Change your view to a strongly typed one, by changing the
Inherits
attribute of thePage
tag on the top to(or whatever the full namespace "path" to the
SelectList
class is...)In your Controller action, you send the
SelectList
to the view as theModel
object, by usingor, if you're returning a view with a different name than the controller action:
In your view, you pass the
Model
object, now strongly typed to aSelectList
because of the View's inheritance, to the helper method:Faster version:
Cast the
ViewData["Estadoslist"]
object to aSelectList
before you send it to the helper method: