ASP.NET Razor页面下拉列表

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

4个回答

3
请尝试以下操作,Buda:
 <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>

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

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接