I am building a school assignment where I demonstrate a form where the user can input information that is then displayed, and they can view existing entries. I have a form where the user inputs data to do with listing a cat for adoption and when they click submit, it should display.
I am encountering the following problem when I try getting the model Cat in my Index view:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Microsoft.AspNetCore.Mvc.Razor.RazorPage.Model.get returned null.
My Index view where the error is occuring:
@model Cat
@{
ViewData["Title"] = "Index";
}
<h2>Cat</h2>
<table class="table table-sm table-bordered table-striped">
<tr><th>name:</th><td>@Model.Name</td></tr>
<tr><th>Age:</th><td>@Model.Age</td></tr>
<tr><th>Colouring:</th><td>@Model.Colouring</td></tr>
<tr><th>KidFriendly:</th><td>@Model.KidFriendly</td></tr>
<tr><th>Personality:</th><td>@Model.Personality</td></tr>
</table>
My Cat Details model:
using System.ComponentModel.DataAnnotations;
namespace IS4439Assignment1.Models
{
public class Cat
{
public int Id { get; set; }
[Required]
public string? Name { get; set; }
//age must be between 1 and 20
[Required]
public int Age { get; set; }
public string? Sex { get; set; }
//user is required to provide colouring details
[Required]
public string? Colouring { get; set; }
//user is required to answer if the cat is kid friendly or not
[Required]
public KidFriendly KidFriendly { get; set; }
//user is required to provide information about cat's personality, string length 100, minimum length 5
[StringLength(100, MinimumLength = 5)]
[Required]
public string? Personality { get; set; }
}
//enum for multiple choice question on form
public enum KidFriendly
{
Yes,
No
}
}
My Home controller:
using IS4439Assignment1.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace IS4439Assignment1.Controllers
{
public class HomeController : Controller
{
public IActionResult Create() => View();
[HttpPost]
public IActionResult Create(Cat model) => View("Index", model);
private IRepository repository;
public HomeController(IRepository repo)
{
repository = repo;
}
public IActionResult Index(Cat catDetails)
{
if (catDetails.Id < 0)
ModelState.AddModelError(nameof(catDetails.Id), "Id cannot be less than 0");
if (string.IsNullOrEmpty(catDetails.Name))
ModelState.AddModelError(nameof(Cat.Name), "Please enter the cats name");
if (catDetails.Age < 0)
ModelState.AddModelError(nameof(catDetails.Age), "Age cannot be less than 0");
if (string.IsNullOrEmpty(catDetails.Sex))
ModelState.AddModelError(nameof(catDetails.Sex), "Please select the cats sex");
//if (catDetails.Colouring.ToString() == "Select")
if (string.IsNullOrEmpty(catDetails.Colouring))
ModelState.AddModelError(nameof(catDetails.Colouring), "Please select the cats colouring");
if (ModelState.IsValid)
return View("Index", catDetails);
else
return View();
}
}
}
I tried to test each line of the Index view where I am getting the model and each line breaks when trying to get the model, with the same error message. I believe the problem could be originating from a nullable value being returned from a dropdown item for the Colouring variable, however I have stated this String variable as nullable in my Cat Details model using
public string? Colouring { get; set; }
This was previously working so I do not understand where the error could have occured from. I am also a beginnner at C# so have little experience.