ASP.NET MVC 5 DropDownListFor not selected

我的梦境 提交于 2020-02-08 05:41:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!