I am working on an ASP.NET Core 5 MVC web application that uses a view model which looks like this:
public class Form3040VM
{
public tblPatientVisits patientVisit = new tblPatientVisits();
public tblPtmstr1 patient = new tblPtmstr1();
public tbl_Log_Vitals patientVitals = new tbl_Log_Vitals();
public FHSAAFindings findings = new FHSAAFindings();
}
The patient class is:
public class tblPtmstr1
{
[Key]
public int PatientID { get; set; }
[Display(Name = "Patient Last Name")]
public string PatientLastName { get; set; }
[Display(Name = "Patient First Name")]
public string PatientFirstName { get; set; }
}
The form renders:
<input id="patient_PatientFirstName" name="patient.PatientFirstName">
as expected. And the form posts a value for patient.PatientFirstName to the controller
public IActionResult FHSAAPreviewSubmit(Form3040VM model)
{
if (ModelState.IsValid)
{
try
{
return Content(model.patient.PatientFirstName ?? "Null Value");
}
catch (Exception ex)
{
return Content(ex.ToString());
}
}
else
{
return Content("Invalid Model");
}
}
The controller returns patient.PatientFirstName as null, even though the console clearly shows a value being posted. However, when I use the IFormCollection and Request.Form["patient.PatientFirstName"] the value is correctly returned.
I'm not sure what the problem is. Any help would be appreciated.