简体   繁体   English

@ this.Html.DropDownList()没有为我选择默认值

[英]@this.Html.DropDownList() does not select default for me

@this.Model.ActiveApplicationId
@this.Html.DropDownList("applications", new SelectList(this.Model.AllApplications, "ApplicationId", "Title", this.Model.ActiveApplicationId))

renders... 呈现...

17
<select id="applications">
    <option value="16">app 16</option>
    <option value="17">app 17</option>
</select

There is no selected="selected" attr on the 2nd option, and there should be. 在第二个选项上没有selected="selected" attr,应该有。 So, by default, since none of them are selected, the 1st one is selected. 因此,默认情况下,由于未选择任何一个,因此选择了第一个。

What am I missing? 我想念什么? Thanks! 谢谢!

Why not use the strongly typed version? 为什么不使用强类型版本? MVC does not respect the Selected value parameter of the SelectList. MVC不遵守SelectList的Selected value参数。

@Html.DropDownListFor(m => m, 
    new SelectList(Model.AllApplications, "ApplicationId", "Title"))

DropDown Helper的第二个参数采用SelectListItem的IEnumerable类型的对象,但是您传递了SelectList类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM