首页 > 解决方案 > C# asp.net 动态填充 list .net 5.0 c# Razor I have been trying for a few days to dynamically populate a <select> list with 164 country names that sit in a DB column. I have worked through various sources online and 'nearly' have it working, but I

问题描述

标签: asp.netlistrazordrop-down-menu

解决方案


问题是:

CountryList = new SelectListItem[]
   {
       new SelectListItem(countriesList[i], countriesList[i])
   };

每次您使用一个元素创建一个新的项目列表时。您应该将所有项目添加到一个列表中。像这样的东西:

public async Task<IActionResult> OnGetAsync(string email)
{
    List<string> countriesList = GetCountriesList.GetCountries();
    Debug.WriteLine("************** countriesListReturned: " + countriesList.ToString());

    CountryList = countriesList.Select(x => new SelectListItem(x, x));

    [normal other code to populate the view goes here]

    return Page();
}

推荐阅读