0

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 displayed on the page is it no longer marked as selected?! I can't see anything else obviously modifying the list.

Thanks :)

Jen
  • 1,964
  • 9
  • 33
  • 59

2 Answers2

2

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 !!

Community
  • 1
  • 1
Jen
  • 1,964
  • 9
  • 33
  • 59
1

Model.Campuses
Model.CampusId
<%= Html.Hidden("CampusId") %>
<%= Html.DropDownList("CampusId", ViewData.Model.Campuses, new { @class = "large search_box" })%>

kimyh00
  • 11
  • 1