问题
I can't get a specific DropDownList to preselect my selected value in ASP.NET MVC 5.
I use the same logic for several other DDLs and it works, but not for this one.
Maybe some of you have an idea.
This piece of code is inside my ASP.NET MVC 5 Razor View:
@Html.DropDownListFor(x => x.SelectedDate, Model.SelectListDays, new { @class="newEntryDays"})
This is what the Watchlist says about my model IN THE VIEW
HTML: The first option is 'selected'
<option selected="selected" value="13.01.2014">Mo (13.01.2014)</option>
I tried ModelState.Clear()
but it didn't help. I also disabled JavaScript (in case my script is the problem) but that didn't help either. :(
It seems like ASP.NET MVC remembers the last state and doesn't change it!
I am overwriting Model.SelectedDate
like this:
if (model.SelectedDateHidden.HasValue)
{
model.SelectedDate = model.SelectedDateHidden.Value;
}
EDIT: I figured this out: Changing it to DropDownList("Blabla") works fine (I guess because it's not bound to my model anymore).
来源:https://stackoverflow.com/questions/21109118/asp-net-mvc-5-dropdownlistfor-not-selected