I have a details-view where a varying number of sites are displayed in textboxes. Next to each site the country should be shown in a disabled dropdown. My problem is that the dropdown does not select the right value. Instead it always selects the first item. Some debugging showed that "SelectedCountries[i]" is holding the correct value for each dropdown but somehow this value does not affect the selection at all.
The dropdown's names are "SelectedCountries[i]" and I've read it is bad to use the same name for the dropdown and for the field in the model. However, changing it didn't solve the problem.
<div id="allSitesAndCountries">
@for (int i = 0; i < Model.SelectedSites.Count; i++)
{
@Html.TextBoxFor(m => m.SelectedSites[i], new { disabled = "disabled", })
@Html.DropDownListFor(m => m.SelectedCountries[i], Model.AllCountriesSelectListItems, new{disabled = "disabled"})
}
</div>
What am I doing wrong here? Thanks in advance.
EDIT: I got it working by creating the SelectList directly in the view and adding the 4th parameter:
<div id="allSitesAndCountries">
@for (int i = 0; i < Model.SelectedSites.Count; i++)
{
<div class="editor-label">
@Html.TextBoxFor(m => m.SelectedSites[i], new { disabled = "disabled"})
@Html.DropDownListFor(m => m.SelectedCountries[i], new SelectList(Model.AllCountries, "Id", "Name", Model.SelectedCountries[i]), new{disabled = "disabled"})
</div>
}
</div>