<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>
所以我的网站上有以下代码。我想要做的是没有一个文本字段,你键入countyID我希望有一个县名下拉列表,用户通过名称选择它,它仍然会注册为数字。 (例如,而不是写&#34; 1&#34;他只需点击并从我制作的列表中选择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 =&gt; model.CountyId和DropDownList选项?
Edit2:我如何使用@ Html.DropDownListFor?
答案 0 :(得分:2)
您可以使用Html.DropDownListFor
辅助方法
向视图模型添加新属性以存储下拉选项。此属性应为List
类型public class CreateUser
{
public int CountryId {set;get;}
public List<SelectListItem> Countries {set;get;}
}
现在,在您的GET操作中,在此视图模型对象上填充Countries集合属性值。
public ActionResult Create()
{
var vm = new CreateUser();
vm.Countries = new List<SelectListItem> {
new SelectListItem { Value="1", Text="USA" },
new SelectListItem { Value="2", Text="Canada" },
new SelectListItem { Value="3", Text="Mexico" }
};
return View(Vm);
}
在你看来
@model CreateUser
@using(Html.BeginForm())
{
<label>Select country</label>
@Html.DropDownListFor(s=>s.CountryId,Model.Countries,"Select one")
<input type="submit" />
}
答案 1 :(得分:1)
答案 2 :(得分:1)
布达,请尝试以下
settings.XmlResolver = new XmlUrlResolver();
答案 3 :(得分:0)
有很多方法可以做到这一点,你可以使用这个:
<div class="form-group col-4">
<label for="issueinput6">category</label>
<select id="issueinput6" class="form-control" data-toggle="tooltip" data-trigger="hover" data-placement="top"
data-title="Status">
@foreach (var item in ViewData["Categoryes"] as IList<AirPortModel.Models.Category>)
{
<option value="@item.Id">@item.CategoryName</option>
}
</select>
</div>
这样,您就可以在下拉菜单中获得所需的所有信息。 这些代码来自我的项目,但您可以更改名称以供您自己使用。 此外,您可以使用此代码图片来解决您的问题。
<select data-val="true" data-val-required="The Category field is required." id="category" name="1">
<option value="">Pick one</option>
<option selected="selected" value="0">blabla</option>
<option value="1">blabla</option>
<option value="2">blabla</option>
<option value="3">blabla</option>
<option value="4">blabla</option>
<option value="5">blabla</option>
<option value="6">blabla</option>