传递到字典中的模型项是类型...但是此字典需要类型的模型项

时间:2022-11-07 04:05:15

I've got an MVC project with a Layout that has strongly typed model named "Dashboard".

我有一个MVC项目,其布局具有强类型模型,名为“Dashboard”。

@model Dashboard_v2.Models.Dashboard

This Layout has a RenderBody() method that renders the rest of the pages.

此布局具有RenderBody()方法,该方法呈现其余页面。

then, on my main page, I have a View that is strongly typed with the Model NewPatient.

然后,在我的主页面上,我有一个使用Model NewPatient强类型的View。

on the controller I have bunch of code, and eventually I pass a NewPatient Model to it.

在控制器上我有一堆代码,最后我将NewPatient模型传递给它。

When I try to run this code, I've got an error called :

当我尝试运行此代码时,我遇到一个错误:

The model item passed into the dictionary is of type 'Dashboard_v2.Models.NewPatient', but this dictionary requires a model item of type 'Dashboard_v2.Models.Dashboard'.

why does it expects a Dashboard Model, if my page has strongly typed NewPatient Model?

如果我的页面强烈键入NewPatient模型,为什么它需要一个仪表板模型?

1 个解决方案

#1


One approach is to define a Viewmodel with generic parameter MainViewModel<T>and instance it with appreciated T for the views. Then you can use the MainVewModel properties in Layout and use properties of T in your views.

一种方法是使用泛型参数MainViewModel 定义Viewmodel,并为视图实例化它。然后,您可以使用Layout中的MainVewModel属性,并在视图中使用T的属性。

#1


One approach is to define a Viewmodel with generic parameter MainViewModel<T>and instance it with appreciated T for the views. Then you can use the MainVewModel properties in Layout and use properties of T in your views.

一种方法是使用泛型参数MainViewModel 定义Viewmodel,并为视图实例化它。然后,您可以使用Layout中的MainVewModel属性,并在视图中使用T的属性。