Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Select Helper Throws Object Reference not set to an instance of an Object

I've created an ASP.NET Core web application that uses Entity Framework Core to read from my existing database. I want to populate a select control with a list of FacilityNames in a view to create a new entry in my FacilityRevenue table.

My Create method on the FacilityRevenue Controller is as follows:

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);
    }

My Create view includes the following markup:

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

My FacilityRevenue Model is as follows:

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

The stack trace is as follows:

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

How do I remedy the above error and achieve the functionality of hydrating the select control with a list of facilities?

like image 440
SidC Avatar asked Nov 18 '16 00:11

SidC


1 Answers

The problem is a typo in the following line:

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

"FacilityID" must be "FacilityId". That is what you defined in your model:

public int? FacilityId { get; set; }
like image 141
jacksparrow92 Avatar answered Oct 13 '22 00:10

jacksparrow92