0

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.

2 Answers 2

1

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.

Please try to modify your viewmodel class Form3040VM to define patient etc as property with get and set accessor, like below.

public class Form3040VM
{
    //...
    public tblPtmstr1 patient { get; set; }
    //...

Then model binding system will help find values from posted data and bind to these public properties. For more information about what model binding is and how it works, you can check this doc:

https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0

Sign up to request clarification or add additional context in comments.

Comments

0

What is the purposeof all these new? I've never seen anything like this before

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();
    }

You just created the empty objects and didn't supply any setters to get any data from submit

try to use this instead

public class Form3040VM
    {
        public tblPatientVisits patientVisit {get; set;}
        public tblPtmstr1 patient {get; set;}
        public tbl_Log_Vitals patientVitals {get; set;}
        public FHSAAFindings findings {get; set;}
    }

and fix the view control bindings too


@model Form3040VM

<form method="post" action="...">

....
<input asp-for="@Model.tblPtmstr1.PatientFirstName"  class="form-control"  />
.... and so on for all controls

</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.