asp.net-core - .NET Core 新的 SelectList 返回 null
问题描述
我正在编写一个 .netcore webapp,我正在使用全球化来填充我的控制器类中的国家列表:
public IActionResult GetCountry()
{
List<string> CountryList = new List<string>();
CultureInfo[] cInfoList = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo cInfo in cInfoList)
{
RegionInfo r = new RegionInfo(cInfo.LCID);
if(!(CountryList.Contains(r.EnglishName)))
{
CountryList.Add(r.EnglishName);
}
}
//sort list into order
CountryList.Sort();
ViewBag.CountryList = CountryList;
return View(CountryList);
}
我的模型类中有以下内容:
[Display(Name ="Country of Origin")]
public string GetCountry { get; set; }
最后在我的cshtml中,我有以下内容:
<div class="form-group">
<label asp-for="GetCountry"></label>
<select asp-for="GetCountry" asp-items="new SelectList(ViewBag.CountryList)"></select>
</div>
我不确定我哪里出错了,但它会在运行时给我以下消息。
ArgumentNullException:值不能为空。参数名称:items Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList..ctor(IEnumerable items, string dataValueField, string dataTextField, IEnumerable selectedValues, string dataGroupField)
我可能做了一些愚蠢的事情,但任何帮助将不胜感激。
解决方案
你错过@
了asp-items
<div class="form-group">
<label asp-for="GetCountry"></label>
<select asp-for="GetCountry" asp-items="@(new SelectList(ViewBag.CountryList))"></select>
</div>
推荐阅读
- css - 如何将导航链接向下移动页面
- .net - 在 .net 框架中更新到 VS 16.5.0 后 HttpContext.Current 为空
- javascript - 未捕获的 TypeError:this.timestamp 不是函数
- javascript - 使用 fetch API 从数组中获取数据的问题
- python - How can I extend file paths for a dictionary
- gitlab - How to Setup GitLab Environment Variables in a safe way?
- node.js - 如何在不使用 NPM 存储库的情况下安装 lerna 包
- c# - EF Core 3.1 查询有时超快但有时很慢
- javascript - How to create a Howerjs progressbar?
- c++ - 我可以使用 emscripten 将 printf 作为 console.warn 吗?