Jen
Jen

Reputation: 2004

My dropdownlist is not selecting an item as expected (MVC 1)

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

Upvotes: 0

Views: 562

Answers (2)

Jen
Jen

Reputation: 2004

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

Upvotes: 2

kimyh00
kimyh00

Reputation: 11

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

Upvotes: 1

Related Questions