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

问题描述

我正在尝试使用 MVC6 标签助手来创建 CountryCode 和 CountryName 的下拉列表,以便用户可以在注册后选择他们的国家.到目前为止,视图的相关部分看起来像这样

<div asp-validation-summary="ValidationSummary.ModelOnly";class="text-danger"></div><select asp-for="CountryCode";[email protected]"></select>

viewmodel 的相关部分看起来像这样

[Display(Name = "Country")]公共字符串 CountryCode { get;放;}公共 IEnumerable国家 { 得到;放;}

一个国家看起来像这样

公共部分类国家{[钥匙]公共字符串 CountryCode { get;放;}公共字符串 CountryName { get;放;}公共虚拟 ICollection用户{得到;放;}}

控制器返回一个国家列表给视图模型

var 模型 = 新的 IndexViewModel{CountryCode = user.CountryCode,国家=_customersContext.Countries.OrderBy(c=>c.CountryName),};返回视图(模型);

但是在视图中 [email protected]" 有一个波浪线 Cannot convert Country to SelectListItem

我也找不到如何在表单中将 CountryCode 指定为要返回的属性,将 CountryName 指定为要显示的属性.

解决方案

我制作下拉菜单的方式有些相似,只是在我的 ViewModel 中,我的属性是 SelectList 类型而不是 IEnumerable.

公共类 HomeViewModel{公共字符串 CountryCode { get;放;}公共选择列表国家列表{获取;放;}}

然后在控制器中我获取数据并将其转换为具有两个属性Id"和Value"的匿名列表.

反过来,我创建了一个新的 SelectList(),传入匿名列表,指定什么是 dataValueField,什么是 dataTextField.

public IActionResult Index(){var country = _customersContext.Countries.OrderBy(c => c.CountryName).Select(x => new {Id = x.Code, Value = x.Name });var model = new HomeViewModel();model.CountryList = new SelectList(countries, "Id", "Value");返回视图(模型);}

最后,在视图中:

<label asp-for="CountryCode" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="CountryCode" asp-items="@Model.CountryList"></select>

I am trying to use MVC6 Tag Helpers to create a dropdownlist of CountryCode and CountryName so that a user can select their country after registering. The relevant part of the view looks like this so far

<form asp-controller="Manage" asp-action="EditCountry" asp-route-returnurl="@ViewData["ReturnUrl"]">
<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<select asp-for="CountryCode" asp-items="@Model.Countries"></select>

The relevant part of the viewmodel looks like this

[Display(Name = "Country")]
public string CountryCode { get; set; }
public IEnumerable<Country> Countries { get; set; }

A Country looks like this

public partial class Country
{
    [Key]
    public string CountryCode { get; set; }
    public string CountryName { get; set; }
    public virtual ICollection<ApplicationUser> Users { get; set; }
}

The controller returns a list of countries to the viewmodel

var model = new IndexViewModel
{
    CountryCode = user.CountryCode,
    Countries =_customersContext.Countries.OrderBy(c=>c.CountryName),
};
return View(model);

but in the view asp-items="@Model.Countries" has a squiggly Cannot convert Country to SelectListItem

Also I cannot find how in the form to specify CountryCode as the property to return and CountryName as the property to display.

解决方案

The way I make my dropdowns is somewhat similar except that in my ViewModel, my property is of type SelectList instead of an IEnumerable<>.

public class HomeViewModel
{
    public string CountryCode { get; set; }
    public SelectList CountryList { get; set; }
}

Then in the controller I get the data and convert it to an anonymous list with two properties "Id" and "Value".

In turn, I create a new SelectList() passing in the anonymous list specifying what is the dataValueField and what is the dataTextField.

public IActionResult Index()
{
    var countries = _customersContext.Countries.OrderBy(c => c.CountryName).Select(x => new { Id = x.Code, Value = x.Name });

    var model = new HomeViewModel();
    model.CountryList = new SelectList(countries, "Id", "Value");

    return View(model);
}

Finally, in the View:

<div class="form-group">
    <label asp-for="CountryCode" class="col-md-2 control-label"></label>
    <div class="col-md-10">
        <select asp-for="CountryCode" asp-items="@Model.CountryList"></select>
    </div>
</div>

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

08-11 03:16