DropDownList setting selected item in asp.net MVC

前端 未结 3 629
长发绾君心
长发绾君心 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: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.

提交回复
热议问题