I have a DropDownList populated with a SelectList generated from (an anonymous type) that has two properties "CountryId" (int) and "Description" (string). I want the selected country in the list to be the defaultCountry.
Countries = new SelectList(countries, "CountryId", "Description", defaultCountry.CountryId);
where "countries" is an IQueryable of anonymous types from a Linq query. Countries is a public property on a view model:
public class CountryBaseViewModel
{
public SelectList Countries { get; set; }
public Contact Contact { get;set; }
....
}
The HTML looks like this:
<p>
<label for="CountryId">Home Country:</label>
<%= Html.DropDownList("CountryId", Model.Countries)%>
</p>
This is fine and when I load the page, the correct country is selected.
But when I submit the change to the Edit method on the controller, the binding seems to fail.
For example, when I use the following HTML
<p>
<label for="Contact.Job">Job/Post:</label>
<%= Html.TextBox("Contact.Job", Model.Contact.Job) %>
</p>
And I call the method
public ActionResult Create(Contact contact)
on the controller, then the contact.Job value is correctly filled in from the TextBox but the CountryId is not.
I presume MVC uses the "Contact.Job" (TextBox 1st) parameter to set this. But the first parameter of the DropDownList is "CountryId" not "Contact.CountryId" because if it is, then the selected value is not shown in the list when I load it.
It looks like the first parameter of TextBox is for data binding and the first parameter of DropDownList is to Find the selected item.
How do I use this style of data binding to get to the selected value I want in the DropDownList and then tie that to Model.Contact.Country?