如何从MVC3的单个视图中的两个表或两个类中获取数据 [英] How to get data from two table or two class in single view in mvc3

查看:83
本文介绍了如何从MVC3的单个视图中的两个表或两个类中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在mvc3的单个视图中从两个表或两个类获取数据.
我想显示两个表中的数据.我有一个家长班和两个孩子班.

How to get data from two table or two class in single view in mvc3.
I want to show data from both the table. I have one parent class and two child class.
i.e

public class GetDocumentParent 
{ 
public DOCUMENTS getDocument { get; set; } 
public DOCUMENT_REVISIONS getDocumentRevision { get; set; }
 }


在此先谢谢您


Thanks in advance

推荐答案

您应该创建一个viewmodel,该模型具有所需的所有数据,并将其传递给视图.这是模型类和页面所需数据收集之间的一层.您只能传入一个模型,因此您需要创建一个包含所有所需数据的类.

另外,如果您使用EF并编写代码以映射到现有数据库(而不是从数据库自动生成代码),则可以扩展数据库模型类,以便DOCUMENTS类可以具有返回所有内容的属性.所选文档的DOCUMENT_REVISIONS.我这样做,我们有一个顶级类,一旦在我的视图模型中有了该类的一个实例,它便具有可以用来获取数据库中存在的所有相关集合的属性.非常干净整洁.
You should create a viewmodel, which is a class that has all the data you need, and pass that to your view. This is a layer between your model classes, and the collection of data needed by a page. You can only pass one model in, so you need to create one class that has all the data you need.

In addition, if you''re using EF and writing code to map to an existing DB ( rather than auto generating code from the DB ), you can extend your DB model classes so that your DOCUMENTS class can have a property that returns all the DOCUMENT_REVISIONS for the selected document. I do this, we have a top level class, and once I have an instance of that in my viewmodel, it has properties I can use to get all related collections that exist in the DB. It''s very neat and clean.


这篇关于如何从MVC3的单个视图中的两个表或两个类中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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