Sid*_*idC 6 c# asp.net-mvc entity-framework asp.net-core
我创建了一个ASP.NET Core Web应用程序,它使用Entity Framework Core从我现有的数据库中读取.我想在视图中填充一个带有FacilityNames列表的select控件,以在我的FacilityRevenue表中创建一个新条目.
我在FacilityRevenue Controller上的Create方法如下:
public IActionResult Create()
{
PopulateFacilityDropDownList();
return View();
}
private void PopulateFacilityDropDownList
(object selectedFacility = null)
{
var facilityQuery = from f in _context.FacilityMaster
orderby f.FacilityName
select f;
ViewBag.FacilityID = new SelectList(facilityQuery.AsNoTracking(), "FacilityID", "FacilityName", selectedFacility);
}
Run Code Online (Sandbox Code Playgroud)
我的"创建"视图包含以下标记:
<label asp-for="FacilityName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="FacilityId" asp-items="ViewBag.FacilityID">
<option value="">-- Select Facility --</option>
</select>
<span asp-validation-for="FacilityName" class="text-danger" />
</div>
Run Code Online (Sandbox Code Playgroud)
我的FacilityRevenue模型如下:
public partial class FacilityRevenue
{
public string FacilityName { get; set; }
public DateTime Date { get; set; }
public decimal? TotalInvoices { get; set; }
public decimal? TotalRevenue { get; set; }
public int RecordId { get; set; }
public int? FacilityId { get; set; }
public virtual FacilityMaster Facility { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪如下:
System.NullReferenceException: Object reference not set to an instance of an
object.
at Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.Eval(Object container,
String expression)
at Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.
GetListItemsWithValueField()
at Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.
GenerateGroupsAndOptions(String optionLabel, IEnumerable`1 selectList,
ICollection`1 currentValues)
at
Microsoft.AspNetCore.Mvc.ViewFeatures.
DefaultHtmlGenerator.GenerateSelect(ViewContext viewContext, ModelExplorer
modelExplorer, String optionLabel, String expression, IEnumerable`1
selectList, ICollection`1 currentValues, Boolean allowMultiple, Object
htmlAttributes)
at
Microsoft.AspNetCore.Mvc.TagHelpers.SelectTagHelper.Process(TagHelperContext
context, TagHelperOutput output)
at
Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.
ProcessAsync(TagHelperContext context, TagHelperOutput output)
at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.
<RunAsync>d__0.MoveNext()
Run Code Online (Sandbox Code Playgroud)
如何解决上述错误并实现使用设施列表对选择控制进行保湿的功能?
Mus*_*aie 11
问题是以下行中的拼写错误:
ViewBag.FacilityID = new SelectList(facilityQuery.AsNoTracking(), "FacilityID", "FacilityName", selectedFacility);
Run Code Online (Sandbox Code Playgroud)
"FacilityID"一定是"FacilityId".这就是您在模型中定义的内容:
public int? FacilityId { get; set; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7773 次 |
| 最近记录: |