<div class="form-group">
@Html.LabelFor(model => model.CountyId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CountyId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CountyId, "", new { @class = "text-danger" })
</div>
</div>
我在我的网站上有以下代码。我想要做的是,不再使用文本字段输入县ID,而是使用县名下拉列表,用户通过名称选择它,并且它仍将被注册为数字。(例如,他不会再写“1”,而是可以从我创建的列表中单击并选择County1)
编辑: 好的,现在我有这段代码:
@Html.DropDownList("Counties", new List<SelectListItem>
{
new SelectListItem {Text = "Alba", Value = "1", Selected = true },
new SelectListItem {Text = "Arad", Value = "2" },
new SelectListItem {Text = "Arges", Value = "3" },
new SelectListItem {Text = "Bacau", Value = "4" },
}, "Select County")
@Html.EditorFor(model => model.CountyId, new { htmlAttributes = new { @class = "form-control" } })
如何编写代码以替换 @Html.EditorFor(model => model.CountyId,使用下拉列表的选择?
编辑2:如何使用 @Html.DropDownListFor ?