本文介绍了ASP.NET Razor 页面下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

@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" })

所以我的网站上有以下代码.我想要做的是,不是有一个文本字段,您可以在其中键入县名,而是希望拥有县名下拉列表,用户可以在其中按名称选择它,并且它仍将注册为数字.(例如,他不会写1",而是点击并从我制作的列表中选择 County1)

好的,现在我有这个代码

@Html.DropDownList("Counties", new List{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" },}, "选择县")@Html.EditorFor(model => model.CountyId, new { htmlAttributes = new { @class = "form-control" } })

我如何编写它来替换 @Html.EditorFor(model => model.CountyId 与 DropDownList 选择?

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

解决方案

您可以使用 Html.DropDownListFor 辅助方法

向您的视图模型添加一个新属性以存储下拉选项.此属性的类型应为 List

公共类 CreateUser{公共 int CountryId {set;get;}公共列表国家 {set;get;}}

现在在您的 GET 操作中,填充此视图模型对象上的 Country 集合属性值.

public ActionResult Create(){var vm = new CreateUser();vm.Countries = new List{新的 SelectListItem { Value="1", Text="USA" },新的 SelectListItem { Value="2", Text="Canada" },新的 SelectListItem { Value="3", Text="Mexico" }};返回视图(Vm);}

在你看来

@model CreateUser@using(Html.BeginForm()){<label>选择国家</label>@Html.DropDownListFor(s=>s.CountryId,Model.Countries,"选择一个")<输入类型=提交"/>}
 <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>

So I have the following code on my website. What I want to do is instead of having a text field where you type the countyID I would like to have the county name dropdown list where the user selects it by name and it would still register as a number. (For example instead of writing "1" he would just click and choose County1 from a list I make)

Edit:Alright now I have this code

@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" } })

How do I write it to replace the @Html.EditorFor(model => model.CountyId with the DropDownList selection?

Edit2: how do I use @Html.DropDownListFor ?

解决方案

You can use the Html.DropDownListFor helper method

Add a new property to your view model to store the dropdown options. This property should be of type List

public class CreateUser
{
  public int CountryId {set;get;}
  public List<SelectListItem> Countries {set;get;}
}

Now in your GET action, populate the Countries collection property value on this view model object.

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);
}

And in your view

@model CreateUser
@using(Html.BeginForm())
{
  <label>Select country</label>
  @Html.DropDownListFor(s=>s.CountryId,Model.Countries,"Select one")
  <input type="submit" />
}

这篇关于ASP.NET Razor 页面下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:35