How to handle models in partial views

1k views Asked by At

In my parent view, I am declaring the model at the top of the view:

@model GEDCPatientPortal.Models.AccountProfileViewModel

Then later in that view, I import a partial view:

@Html.Partial("~/Views/Shared/_SelectPatientScheduleAppointment.cshtml")

The problem I'm running into is that this partial view has it's own model declared, because the partial view has a dropdown in it which I want to be strongly typed...

@model GEDCPatientPortal.Models.PatientPortalViewModels
....
@Html.DropDownListFor(model => Model.SelectPatient)

I understand why I am getting an error, I'm just not sure how to work around it.

Error: The model item passed into the dictionary is of type 'GEDCPatientPortal.Models.AccountProfileViewModel', but this dictionary requires a model item of type 'GEDCPatientPortal.Models.PatientPortalViewModels'

1

There are 1 answers

4
Jens R. On BEST ANSWER

Use

@Html.Partial("~/Views/Controller/View.cshtml", model)

to pass the required model into your partial view.

You can make the partial view's model part of the main view's model (e.g. a property AccountProfileViewModel.PatientPortal) and pass that into the partial view.