DropdownListFor default value

前端 未结 5 515
無奈伤痛
無奈伤痛 2020-11-28 06:38

Is there a simple way to add a \"--Please select--\" default option to a DropDownListFor in MVC 3?

5条回答
  •  执念已碎
    2020-11-28 07:00

    I wanted to set the default value to whatever was passed in as a Url Parameter called SiteType:

        
    @Html.LabelFor(model => model.Type, new { @class = "control-label col-md-2" })
    @Html.DropDownListFor(model => model.Type, ChangeOrderSite.SiteTypeNames.Select(s => new SelectListItem { Text = s.Value, Value = s.Key.ToString(), Selected = s.Key.ToString() == Request["SiteType"] }), new { @class = "control-label col-md-2" }) @Html.ValidationMessageFor(model => model.Type)

    My drop down is a list of Site Types.

提交回复
热议问题