MVC6 国家下拉列表

MVC6 Dropdownlist of Countries

我正在尝试使用 MVC6 Tag Helpers 创建一个 CountryCode 和 CountryName 的下拉列表,以便用户在注册后可以 select 他们的国家。到目前为止,视图的相关部分看起来像这样

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

视图模型的相关部分如下所示

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

一个国家看起来像这样

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

控制器 return 是视图模型的国家列表

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

但在视图中 asp-items="@Model.Countries" 有波浪形 Cannot convert Country to SelectListItem

我也找不到如何在表格中将国家代码指定为 属性 到 return 并将国家名称指定为 属性 以显示。

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

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

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

反过来,我创建了一个新的 SelectList() 传入匿名列表,指定什么是 dataValueField 和什么是 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);
}

最后,在视图中:

<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>
public class MyViewModel  //MODEL LAYER
    {
        public int CountryId { get; set; }

    public string CountryName { get; set; }
    public List<Employee> EmployeesList { get; set; }
}

public class Employee
{
    public int Id { get; set; }
    public string FullName { get; set; }
}

public IActionResult Contact1()  //CONTROLLER
        {

        MyViewModel N1 = new MyViewModel();
        List<Employee> N2 = new List<Employee>()
        {
            new Employee { Id=1,FullName="sivaragu" },
             new Employee { Id=2,FullName="siva" },
                  new Employee { Id=3,FullName="SENTHIL" }
        };

        ViewBag.MovieType = N2;
        return View();            
    }

CSHTML(MVC6)

<select  asp-for="CountryId" asp-items="@(new SelectList(@ViewBag.MovieType,"Id","FullName") )">    
    </select>

我提出了一种替代方法,通过使用更多属性扩展 SelectTagHelper 可以使此类开发更加方便。讨论了这个问题 here

它基于class SelectListDescriptor 包含项目列表, 属性 分别显示文本和值字段。然后在视图中一个简单的类型

<select asp-descriptor="@ViewBag.CountryDescriptor"><option value="">-- Choose country</option<</select>.

国家/地区描述符只是 new SelectListDescriptor(nameof(Country.CountryCode), nameof(Country.CountryName), countries)。这通过利用 `nameof-operator

的力量避免了魔法字符串