DropDownList setting selected item in asp.net MVC

前端 未结 3 618
长发绾君心
长发绾君心 2020-11-27 16:48

I noticed what seems to me a bug in asp.net MVC or simply I am doing something wrong. I am currently using 1.0 so maybe this is something that will be addressed in the 2.0 r

相关标签:
3条回答
  • 2020-11-27 17:08

    I think the problem is a confusion regarding the DropDownList overloads:

    1. Html.DropDownList(string name) looks for a view model property of name and type IEnumerable<SelectListItem>. It will use the selected item (SelectListItem.Selected == true) from the list, unless there is a form post value of the same name.

    2. Html.DropDownList(string name, IEnumerable<SelectListItem> selectList) uses the items from selectList, but not their selected values. The selected is found by resolving name in the view model (or post data) and matching it against the SelectListItem.Value. Even if the value cannot be found (or is null), it still won't use the selected value from the list of SelectListItems.

    Your code uses the second overload, but specifies a "value" property that doesn't exist ("MultipleServicers").

    To fix your problem, either use the first overload:

    <%= Html.DropDownList("IsMultipleServicers") %>
    

    Or, add a string MultipleServicers property to your view model and populate it in your controller. I'd recommend this solution as it gets around several problems with initial display, post display and mapping the post data to a view/post model:

    public class ServiceViewModel : ViewModel 
    { 
         public string MultipleServicers { get; set; } 
         public List<SelectListItem> IsMultipleServicers { get; set; } 
    }
    

    Then for your HTML:

    <%= Html.DropDownList(Model.MultipleServicers, Model.IsMultipleServicers) %>
    

    This technique maps into MVC2, as well:

    <%= Html.DropDownListFor(x => x.MultipleServicers, Model.IsMultipleServicers) %>
    
    0 讨论(0)
  • 2020-11-27 17:20

    I encountered this same problem using the Html.DropDownList(string name, IEnumerable selectList) overload. It appears that my model has a property of the same name as the name of the drop down list. This being the case, MVC favored the property value of my Model over the Selected property of each entry in the IEnumerable.

    The solution was to use a name for the dropdown list that does not match up to a property name. Another solution would be to write my own extension method that ignores model and view state and instead always honor the selected property.

    0 讨论(0)
  • 2020-11-27 17:28

    The DropDownList helper pulls the default value from the model. In the first case, the value in the model corresponding to the name is a SelectList -- this doesn't match any of the items in the list, it is the list, so no value is chosen. In the second example, your model does not include a property with that name so the value from the model can't be used and it defaults to the state indicated in the SelectList itself. Typically, I will have a property on the model for the selected value -- this becomes the default -- and another property representing the potential values for the list.

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