In my C# .NET project I have 2 pages, and when I load my results page there is an error saying Exception Thrown for a variable that was supposed to be chosen from a select list:
System.NullReferenceException: 'Object reference not set to an instance of an object. Microsoft.AspNetCore.Mvc.Razor.RazorPage.Model.get returned null.
What I'm trying to do is have a county chosen from the selectlist and display it on the results page in a strongly-typed view so my expected output is the name of the county chosen.
I have tried adding a payload to save the value if it was getting nulled out but that didn't work at all. So now I am suspecting something is wrong with the selectlist itself because I wasn't sure if I was setting it up correctly with all the different options of what to add in a selectlist.
Here is the selectlist in my controller:
model.CountySelectList = new SelectList(model.Counties, "CountyName", "CountyName", model.SelectedCounty);
Here is the selectlist in the index page:
<select class="form-control form-select" id="county"
asp-for="CountySelectList" asp-items="Model.CountySelectList" name="selectedCounty">
<option value="">Please Select One</option>
</select>
Here is the code in the results page:
<p>County: @Model.SelectedCounty </p>
I'm thinking that the problem is somewhere in the selectlist in the controller, like the way I'm trying to have the property of CountyName be what gets passed on, but I'm not sure. Any insight is appreciated. Thank you.