Solving development problems  |  About this blog

Archive for the ‘web programming’ tag

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);

Written by Avivo

August 17th, 2009 at 3:27 pm