I am trying to display a dropdownlist for project status, that also populates with the current value in the database. The problem is that I keep getting a null reference exception on the UpdateProject
action (when the button is clicked).
The error in VS2022 is
Microsoft.AspNetCore.Mvc.Razor.RazorPage.Model.get returned null.
The view model is
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;
using WebServices.DomainModels.Projects;
namespace WebServices.ViewModels.Projects
{
public class vmUpdateProject
{
public Project Projects { get; set; }
public IEnumerable<SelectListItem> Priorities { get; set; }
public string SelectedPriority { get; set; }
public IEnumerable<SelectListItem> Statuses { get; set; }
public string SelectedStatus { get; set; }
public IEnumerable<SelectListItem> Urgency { get; set; }
public string SelectedUrgency { get; set; }
}
}
and the view segment is
<form method="post" asp-action="UpdateProject" class="container-fluid ps-5">
<input asp-for="Projects.ProjectId" />
<div class="container-fluid p-3">
<div class="row">
<div class="col-lg-8">
<h1 class="text-primary">Update Project Details</h1>
</div>
<div class="col-lg-2">
<input type="submit" class="btn btn-info w-100" value="Update Project" />
@*<a asp-action="UpdateProject" class="btn btn-info w-100">Update Project</a>*@
</div>
<div class="col-lg-2">
<a asp-action="Dashboard" asp-controller="Project" class="btn btn-danger w-100">Project Dashboard</a>
<p class="small">Returns you to the dashboard. You will lose any changes</p>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 float-right">
<div class="col-lg-6">
Project Status
</div>
<div class="col-lg-6">
@Html.DropDownListFor(m => Model.Projects.ProjectStatus, new SelectList(Model.Statuses,"Value","Text",Model.Projects.ProjectStatus))
</div>
</div>
</div>
</form>
The view displays and fills all the data in fine. The DropDownList is populated with the list items, but when I click Update Project, it gets a null reference exception on the line @Html.DropDownListFor section and I have no idea what I am missing that is causing this.
The controller GET action is
public IActionResult UpdateProject(int? id)
{
if (id == null || id == 0)
{
return NotFound();
}
var vmProjectDetails = new vmUpdateProject();
vmProjectDetails.Projects = ReturnProjectDetails(id, dbWebServicesString);
vmProjectDetails.Priorities = GetSelectListItems("ProjectPriority", dbWebServicesString);
vmProjectDetails.Statuses = GetSelectListItems("ProjectStatus", dbWebServicesString);
vmProjectDetails.Urgency = GetSelectListItems("ProjectUrgency", dbWebServicesString);
//var obj = _db.Projects.Find(id);
//if(obj == null)
//{
// return NotFound();
//}
return View(vmProjectDetails);
}
The post action is:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult UpdateProject(vmUpdateProject obj)
{
try
{
if (ModelState.IsValid)
{
_db.Projects.Update(obj.Projects);
_db.SaveChanges();
return RedirectToAction("Dashboard", "Project");
}
else
{
//TODO: Put in error handling for ModelState.IsValid
return View();
}
}
catch (SqlTypeException ex)
{
return null;
}
}
Regards, Darren