ASP.NET MVC dropdownlist not selecting value on render

前端 未结 8 739
感动是毒
感动是毒 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:34

    Just ensure that the variable name set on ViewBag is different to the name assigned to the dropdownlist - i.e. don't do:

    Controller:

    ViewBag.CityId = new SelectList(....)

    View:

    @Html.DropDownList("CityId", ...

    Just use a different viewbag variable name.

    I've had problems when using the same ViewBag variable name as a html control name with drop down lists and listboxes.

提交回复
热议问题