视图中的ASP.Net Core 2.1 MVC SelectList未被填充 [英] ASP.Net Core 2.1 MVC SelectList in View is not getting populated
问题描述
我正在ASPNet Core 2.1 MVC网站上工作,我试图获取一个选择列表以填充公司列表(值= Id,文本= CompanyName)。我可以看到该数据存在于ViewModel的列表中,但是无论我如何尝试,都无法使该数据显示在视图的下拉列表中。
我正在从
这是我视图中的选择
< div class = form-group>
< label asp-for = CompanyName>< / label>
<选择asp-for = Id asp-item = @(new SelectList(Model.CompanyList, Id, CompanyName)) class = form-control>
< option>请选择公司< / option>
< / select>
< / div>
以下是屏幕截图,显示了视图模型此时在视图中填充的数据。
这是当我打开网页上的下拉列表时的截图,其中没有公司。
这似乎应该非常直截了当。我在这里缺少什么?
<选择asp-for = Id asp-item = @(new SelectList(Model.CompanyList, Id, CompanyName)) ...>
选择标记帮助程序的方式类似于 asp-item
,但应为 asp项
。
选择标记帮助器 asp-for
指定select元素的模型属性名称,而 asp项
指定选项元素。
在此处查看此文档: https://docs.microsoft.com/zh-CN/aspnet/core/ mvc / views / working-with-forms?view = aspnetcore-2.1
I am working on an ASPNet Core 2.1 MVC website and I am trying to get a select list to populate with a list of Companies (value = Id, text = CompanyName). I can see that the data exists in the List in the ViewModel but no matter what I try, I can't get the data to show in the dropdown list in the view.
I am following the first variation of the SelectList recommendation from this SO post
Here is my EditSite ViewModel class
public class EditSite
{
public int Id { get; set; }
[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 2)]
[Display(Name = "Site Name")]
public string SiteName { get; set; }
[Display(Name = "Enabled")]
public bool IsEnabled { get; set; }
public string ReturnUrl { get; set; }
public List<CompanySelect> CompanyList { get; set; }
}
Here is my CompanySelect moodel class
public class CompanySelect
{
public int Id { get; set; }
public string CompanyName { get; set; }
}
Here is my Edit action in my Sites Controller;
public async Task<IActionResult> Edit([FromQuery] int id, string returnUrl = null)
{
await SetCurrentUser();
ViewData["Role"] = _currentRole;
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
var response = await _client.GetSiteAsync(id, $"api/companies/{SetCompanyId()}/sites/{id}", token);
if (response == null)
{
return NotFound($"Unable to find a record for Company ID [{id}].");
}
var site = _mapper.Map<EditSite>(response.Site);
if (_currentRole != UserRoles.SysAdmin) return View(site);
site.CompanyList = await GetCompaniesForSelectList();
return View(site);
}
Here is a screenshot when I debug, showing that the site.CompanyList is populated with data.
Here is the Select in my my View
<div class="form-group">
<label asp-for="CompanyName"></label>
<select asp-for="Id" asp-item="@(new SelectList(Model.CompanyList, "Id","CompanyName"))" class="form-control">
<option>Please select a Company</option>
</select>
</div>
Here is a screenshot showing the viewmodel has the data populated at this point in the view.
And here is a screenshot when I open the dropdown list on the web page, showing no companies.
This seems like it should be very straight forward. What am I missing here?
<select asp-for="Id" asp-item="@(new SelectList(Model.CompanyList, "Id","CompanyName"))" ...>
The select tag helper is written like asp-item
but it should be asp-items
.
The select tag helper asp-for
specifies the model property name for the select element and asp-items
specifies the option elements.
See this documentation here: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-2.1
这篇关于视图中的ASP.Net Core 2.1 MVC SelectList未被填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!