加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.NET Core Select Helper引发对象引用未设置为Object的

发布时间:2020-12-15 23:36:08 所属栏目:百科 来源:网络整理
导读:我创建了一个ASP.NET Core Web应用程序,它使用Entity Framework Core从我现有的数据库中读取.我想在视图中填充一个带有FacilityNames列表的select控件,以在我的FacilityRevenue表中创建一个新条目. 我在FacilityRevenue Controller上的Create方法如下: publ
我创建了一个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);
    }

我的“创建”视图包含以下标记:

<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>

我的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; }
}

堆栈跟踪如下:

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()

如何解决上述错误并实现使用设施列表对选择控制进行保湿的功能?

解决方法

问题是以下行中的拼写错误:

ViewBag.FacilityID = new SelectList(facilityQuery.AsNoTracking(),selectedFacility);

“FacilityID”必须是“FacilityId”.这就是您在模型中定义的内容:

public int? FacilityId { get; set; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读