Search code examples
c#asp.net-coreasp.net-core-mvcidentityargumentnullexception

How to manage NullReferenceException?


I want to make a form that user have to fill out it. But I'm getting an error.

Model:

public UserReport Input;

[Key]
//[Required]
public string ReportID { get; set; }


[Required]
public string Description { get; set; }

[Required]
[DataType(DataType.DateTime)]
public string Date { get; set; }

Controller:

private ApplicationDbContext _userReport;
public UserReport Input;

public PageController(ApplicationDbContext userReport)
{
    _userReport = userReport;
}

[HttpGet]
public IActionResult SendReport()
{
    return View();
}

[Authorize]
[HttpPost]
public async Task<IActionResult> SendReport(UserReport userReport)
{
    var user = new UserReport { Description = Input.Description, Date = Input.Date };
    var r = await _userReport.AddAsync(user);
}

View:

@model UserReport
<div class="col-md-6">
    <form id = "SendReport" method="post">
        <div asp-validation-summary="All" class="text-danger"></div>

        <div class="form-group">
            <label asp-for="Input.Description"></label>
            <input asp-for="Input.Description" class="form-control" />
            <span asp-validation-for="Input.Description" class="text-danger"></span>
        </div>

        <div class="form-group">
            <label asp-for="Input.Date"></label>
            <input asp-for="Input.Date" class="form-control" />
            <span asp-validation-for="Input.Date" class="text-danger"></span>
        </div>


        <button type = "submit" class="btn btn-primary">send report</button>
    </form>
</div>

what is wrong with that? It shows me this Error :System.NullReferenceException - Object reference not set to an instance of an object... who can help me to manage this error. Show me where is my mistake please


Solution

  • You have mixed with MVC and Razor Pages.

    For MVC:

    1.Model:

    public class UserReport
    {
        [Key]
        //[Required]
        public string ReportID { get; set; }
    
        [Required]
        public string Description { get; set; }
    
        [Required]
        [DataType(DataType.DateTime)]
        public string Date { get; set; }
    }
    

    2.SendReport.cshtml:

    @model UserReport
    <div class="col-md-6">
        <form asp-action="SendReport" method="post">
            <div asp-validation-summary="All" class="text-danger"></div>
    
            <div class="form-group">
                <label asp-for="Description"></label>
                <input asp-for="Description" class="form-control" />
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
    
            <div class="form-group">
                <label asp-for="Date"></label>
                <input asp-for="Date" class="form-control" />
                <span asp-validation-for="Date" class="text-danger"></span>
            </div>
    
    
            <button type="submit" class="btn btn-primary">send report</button>
        </form>
    </div>
    

    3.Controller:

    public class UserReportsController : Controller
    {
        private readonly YourDbContext _context;
    
        public UserReportsController(YourDbContext context)
        {
            _context = context;
        }
        public IActionResult SendReport()
        {
            return View();
        }
    
        [HttpPost]
        public async Task<IActionResult> SendReport([Bind("ReportID,Description,Date")] UserReport userReport)
        {
            if (ModelState.IsValid)
            {
                _context.Add(userReport);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(userReport);
        }
    }
    

    For Razor Pages:

    1.Model:

    public class UserReport
    {
        [Key]
        //[Required]
        public string ReportID { get; set; }
    
    
        [Required]
        public string Description { get; set; }
    
        [Required]
        [DataType(DataType.DateTime)]
        public string Date { get; set; }
    }
    

    2.SendReport.cshtml:

    @page
    @model SendReportModel
    
    <div class="col-md-6">
        <form id="SendReport" method="post">
            <div asp-validation-summary="All" class="text-danger"></div>
    
            <div class="form-group">
                <label asp-for="Input.Description"></label>
                <input asp-for="Input.Description" class="form-control" />
                <span asp-validation-for="Input.Description" class="text-danger"></span>
            </div>
    
            <div class="form-group">
                <label asp-for="Input.Date"></label>
                <input asp-for="Input.Date" class="form-control" />
                <span asp-validation-for="Input.Date" class="text-danger"></span>
            </div>
    
    
            <button type="submit" class="btn btn-primary">send report</button>
        </form>
    </div>
    

    3.SendReport.cshtml.cs:

    public class SendReportModel : PageModel
    {
        private readonly YourDbContext _context;
    
        public SendReportModel(YourDbContext context)
        {
            _context = context;
        }
    
        public IActionResult OnGet()
        {
            return Page();
        }
    
        [BindProperty]
        public UserReport Input { get; set; }
    
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
    
            _context.UserReport.Add(Input);
            await _context.SaveChangesAsync();
    
            return RedirectToPage("./Index");
        }
    }
    

    Reference: