MVC Dropdown list value not being set

前端 未结 2 571
一生所求
一生所求 2021-01-19 16:54

I\'m sure I am missing something obvious, but this is driving me nuts! If I specify html options, the value of my dropdownlist doesn\'t set.

In my controller, I ret

相关标签:
2条回答
  • 2021-01-19 17:41

    the second parameter of the DropDown Helper takes an object of type IEnumerable(Of SelectListItem) but you passed an object of type IEnumerable(Of SelectList) so this how the code should be written:

         <%=Html.DropDownList("Coordinator",((SelectList)ViewData["Coordinator"]).AsEnumerable(),
     new {style="width:175px"}) %>
    
    0 讨论(0)
  • 2021-01-19 17:42

    I was struggling with a similar problem yesterday so if you are still getting the same result there is one more thing to consider. The DropDownList sometimes ignores the selected value of your SelectList, it's annoying but what it does is try to get the selected value from the ModelState, ViewData and Model by using the field name as a key. In your case you are storing the list in ViewData["Coordinator"], the key has the same name as the DropDown. Try this:

    ViewData["CoordinatorList"] = new SelectList(userRepository.GetIdUserList(1), 
                              "ID",    "Signature",edCenter.Coordinator);
    
    ViewData["Coordinator"] = dCenter.Coordinator;
    

    Then in the view:

        <%=Html.DropDownList("Coordinator",((SelectList)ViewData["CoordinatorList"]).AsEnumerable(),
     new {style="width:175px"}) %>
    

    If you want to see what's going on behind the curtains open reflector (or get the MVC source) and browse this method: System.Web.Mvc.Html.SelectExtensions.SelectInternal()

    0 讨论(0)
提交回复
热议问题