gpt4 book ai didi

c# - ASP.NET Razor 页面下拉列表

转载 作者:行者123 更新时间:2023-12-04 00:11:35 24 4
gpt4 key购买 nike

 <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>

所以我的网站上有以下代码。我想要做的是,而不是有一个文本字段,您可以在其中键入县名,我希望拥有县名下拉列表,用户可以在其中按名称选择它,并且它仍将注册为数字。 (例如,他不会写“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 与 DropDownList 选择?

Edit2:我如何使用 @Html.DropDownListFor ?

最佳答案

布达,请试试下面

 <div class="form-group">
@Html.LabelFor(model => model.CountyId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CountyId, 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" },
}, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CountyId, "", new { @class = "text-danger" })
</div>
</div>

关于c# - ASP.NET Razor 页面下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34482323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com