Solving development problems  |  About this blog

SelectedValue not working in ASP.NET MVC

<%= Html.DropDownList(“Salutation”, (IEnumerable<SelectListItem>)ViewData["Salutation"])%>

We just did not use properties in our viewdata that had the same name as any of the dropdowns , so above example is wrong and it will not select value properly. Instead of this use something like this:

<%= Html.DropDownList(“Salutation”, (IEnumerable<SelectListItem>)ViewData["SalutationList"])%>

And in controller:

ViewData["SalutationList"] = new SelectList(userModel.Salutations, “Value”, “Title”, user.Salutation);

August 17th, 2009

blog comments powered by Disqus