如何处理部分视图中的模型

How to handle models in partial views

在我的父视图中,我在视图顶部声明模型:

@model GEDCPatientPortal.Models.AccountProfileViewModel

然后在该视图的后面,我导入了一个局部视图:

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

我 运行 遇到的问题是这个局部视图声明了它自己的模型,因为局部视图中有一个下拉列表,我希望它是强类型的...

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

我明白为什么会出现错误,只是不确定如何解决它。

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'

使用

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

将所需的模型传递到您的局部视图中。

您可以将局部视图的模型作为主视图模型的一部分(例如 属性 AccountProfileViewModel.PatientPortal)并将其传递到局部视图中。