如何在一个视图中使用多个模型? [英] How to use multiple model in one view?

查看:99
本文介绍了如何在一个视图中使用多个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

您能帮我,如何在一个MVC视图中使用多个模型?

例如,我有两个班级,分别是项目",会议".我想以一种视图向他们展示这是我的项目...."和这是我的会议",但是我有这样的错误;

传递到字典中的模型项的类型为< system.collections.generic>
但是此词典需要类型为["Projects"]的模型项."

感谢您的帮助,

祝你有美好的一天.

Hello,

Can you help me, how to use multiple model in one mvc view?

for example i have two classes called Projects, meetings. i want to show them in one view "Here is my projects...." and "here is my meetings" but i have an error like this;

"The model item passed into the dictionary is of type <system.collections.generic>
but this dictionary requires a model item of type ["Projects"]."

thanks for your help,

have a nice day.

推荐答案

MVC模式的工作方式是:您拥有一个模型"(即数据),并且可以使用不同的视图"进行显示"(例如表格,图形...),但特定视图仅处理特定模型.

对于您的情况,可以执行以下操作之一:

1)使项目"和会议"在视图中看起来相同.

1.1)您可以通过从相同的基类派生它们(将此类称为"Displayable"基类)并准备视图以显示Displayable项或...来实现此目的.
1.2) ...通过实现相同的接口(将此类接口称为"IDisplayable"),并准备好视图以处理实现IDisplayable接口的项目.

2)创建不同的视图,每个模型一个(一个用于项目"列表,另一个用于会议"列表).


错误消息...

传递到字典中的模型项为
类型 但是此词典需要类型为["Projects"]的模型项."


...表示您的视图仅知道项目"项(或从项目派生而来的内容),而会议" 不是(意思是,不是从项目"继承).
The way the MVC pattern works is: you have a "model" (i.e., data) and you can show it with different "views" (e.g., table, graph...), but a particular view handles only a specific model.

In your case, you may do one of the following:

1) Make "Project" and "Meeting" look the same to the View.

1.1) You can do this by deriving them from the same base class (lets call such base class "Displayable") and prepare your View to show Displayable items or...

1.2) ... by implementing the same interface (lets call such interface "IDisplayable") and prepare your View to handle items implementing the IDisplayable interface.

2) Create different views, one for each model (one for Project lists, and another for Meeting lists).


The error message...

"The model item passed into the dictionary is of type
but this dictionary requires a model item of type ["Projects"]."


... suggests your View only knows "Project" items (or something derived from Projects) and that a "Meeting" is not (meaning, doesn''t inherit from) a "Project".


这篇关于如何在一个视图中使用多个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆