OK so I have 2 pages where this kind of behaviour is implemented. It works on one but doesn't work in another and I have no idea why. Despite the data containing a list of selectitems and one of them is selected the dropdown list is not displaying this selection (ie. it resets to the blank item).
I don't know how to further debug this.
In my page:
<%= Html.DropDownList("CampusId", ViewData.Model.Campuses, new { @class = "large search_box" })%>
In my controller.
Campuses = AdminRepository
.ListAll<Campus>(a => a.Description)
.ToSelectListItem<Campus>(a => a.CampusId, a => a.Description, criteria.CampusId, true);
I can see that campuses does have the correct list item marked as selected - so why when it is开发者_JAVA百科 displayed on the page is it no longer marked as selected?! I can't see anything else obviously modifying the list.
Thanks :)
OK so in trying different google search terms I came across this: ASP.NET MVC Html.DropDownList SelectedValue
The issue was my drop down list had the same name as in the model. So I changed my html to be
<%= Html.DropDownList("CampusIdDD", ViewData.Model.Campuses, new { @class = "large search_box" })%>
So problem solved!!! Hopefully we'll be able to upgrade from MVC 1 !!
Model.Campuses
Model.CampusId
<%= Html.Hidden("CampusId") %>
<%= Html.DropDownList("CampusId", ViewData.Model.Campuses, new { @class = "large search_box" })%>
精彩评论