这个问题已经在这里有了答案 :
MVC5-如何在DropDownListFor Html helper中设置“ selectedValue” (3个答案)
2年前关闭。
@Html.DropDownListFor(model => model.Status, new List<SelectListItem> { new SelectListItem{Text="Active", Value="True"}, new SelectListItem{Text="Deactive", Value="False"}})
鉴于我正在使用这种下拉dowenlist编码。我运行我的应用程序,默认非活动值显示在下拉列表框中。我想显示默认的 活动
像这样:
@Html.DropDownListFor(model => model.Status, new List<SelectListItem> { new SelectListItem{Text="Active", Value="True"}, new SelectListItem{Text="Deactive", Value="False"}},"Select One")
如果要默认选择“活动”,则使用以下Selected属性SelectListItem:
Selected
SelectListItem
@Html.DropDownListFor(model => model.Status, new List<SelectListItem> { new SelectListItem{Text="Active", Value="True",Selected=true}, new SelectListItem{Text="Deactive", Value="False"}},"Select One")
如果使用SelectList,则必须使用此重载并指定SelectListItem Value要设置为selected的属性:
SelectList
Value
@Html.DropDownListFor(model => model.title, new SelectList(new List<SelectListItem> { new SelectListItem { Text = "Active" , Value = "True"}, new SelectListItem { Text = "InActive", Value = "False" } }, "Value", // property to be set as Value of dropdown item "Text", // property to be used as text of dropdown item "True"), // value that should be set selected of dropdown new { @class = "form-control" })