Я пишу веб-приложение .netcore, и я использую глобализацию для заполнения списка стран, которые находятся в моем классе контроллера:
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: Value cannot be null. Parameter name: 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>
Я добавил @ (), но, к сожалению, у меня все еще та же ошибка