0

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.

Andrew Corrigan
  • 1,017
  • 6
  • 23
Elissa121
  • 1
  • 5
  • It looks like you're using MVC/Razor - it might help you get better answers if you include those tags – Andrew Corrigan May 11 '22 at 14:25
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – madreflection May 11 '22 at 15:21
  • madreflection - no it doesn't because I'm specifically asking if the way I'm structuring my selectlist is the cause of the value being null – Elissa121 May 11 '22 at 15:31
  • The things you say you've tried, you're just guessing. The exception says that `Model.get` returned `null`, so you need to fix that. How does a value get into `Model`? By passing the value to `View()` in the action method. This is a common omission. – madreflection May 11 '22 at 15:31
  • The question about how you're structuring your selectlist is irrelevent to the exception. – madreflection May 11 '22 at 15:34
  • I tried to post the minimal amount of code, but I was trying to use the controller to do this both with and without a payload and temp data. – Elissa121 May 11 '22 at 15:57
  • Your action method is probably doing `return View();` when it should be doing `return View(model);` or something like that. What's the action method returning? – madreflection May 11 '22 at 15:59
  • (By the way, "payload" is not a term that someone is going to instantly understand as a specific thing in this context. You should be more specific about what that payload is and what you're doing with it.) – madreflection May 11 '22 at 16:03

0 Answers0