Tag Helper Select 元素是空的,即使它的数据源不是

Tag Helper Select element is empty even though its data source is not

我正在使用 ASP.NET 核心 MVC RC2 项目使用 "Database First Model"。数据库是大名鼎鼎的Northwind数据库。一切正常,除了应该用 ViewBag 数据填充的 "Select Tag Helper"(一个 HTML 下拉列表)是空的,即使 ViewBag 数据是 为空:

CustomersController 上的以下操作方法正确显示了所有客户的列表:

public IActionResult Index()
{
    List<Customers> data = repository.SelectAll();
    return View("/views/Northwind/Index.cshtml", data);
}

当用户单击记录上的 "Edit" link 时,以下 Edit 操作会在编辑表单中正确显示该记录值 除了下拉 - down for countries 为空:

public IActionResult Edit(string id)
{
     Customers data = repository.SelectByID(id);

     var query = (from c in repository.Context.Customers
                    orderby c.Country ascending
                    select new SelectListItem() { Text = c.Country, Value = c.Country }
                 ).Distinct();

   List<SelectListItem> countries = query.ToList();
   ViewBag.Countries = countries;
   return View("/views/Northwind/Edit.cshtml", data);
}

下面是显示可编辑数据的"view"除了下拉是空白:

@model MVCCoreCR2_Project.Models.Customers

<div class="form-group">
  <label asp-for="Country" class="col-md-2 control-label"></label>
  <div class="col-md-10">
    <select asp-for="Country" asp-items="@ViewBag.Countries" class="form-control" />
   <span asp-validation-for="Country" class="text-danger" />
  </div>
</div>

注意:在调试过程中,当我在 <select asp-for="Country" asp-items="@ViewBag.Countries" class="form-control" /> 下一个断点并将鼠标悬停在 @ViewBag.Countries 上时,我可以看到 ViewBag 中的国家是填写.

A <select> 不是自闭标签。必须

<select asp-for="Country" asp-items="ViewBag.Countries" class="form-control"></select>