ASP.NET MVC dropdownlist not selecting value on render

前端 未结 8 731
感动是毒
感动是毒 2020-12-29 12:56

I have this annoying problem where my DropDownlist doesn\'t select the current value by default.

Controller:

var YearsCycling = new SelectList(new Li         


        
8条回答
  •  -上瘾入骨i
    2020-12-29 13:31

    If you are rendering your list like this: @Html.DropDownListFor(model => model.EntityID, Model.EntitySelectList)

    The value .NET MVC will use when rendering the select options will match the value of model.EntityID.ToString().

    ... It will not select the Model.EntitySelectList.SelectedValue

    ... Or any of the EntitySelectList.Item.SelectedValue items.

    ... In other words, when rendering a DropDownListFor something other than the list itself, the SelectedValue properties of both the List and the List Items are ignored.

    No matter what Model.EntitySelectList.SelectedValue or Model.EntitySelectList.Item.SelectedValue is set, ASP.NET MVC will not render selection. Use the model.EntityID instead.

提交回复
热议问题