如何在单个视图中传递多个类数据? [英] how to pass multiple class data in single view?

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

问题描述

大家好,

我有两个班级,A级,B级

两者都包含一些用户数据.我想在视图中显示该数据.所以我的问题是我们如何在一个视图中传递多个类数据.因为一个视图只有一个模型.

我不想为此创建新的类.还可以使用Entity Framework

Hi all,

I have two classes, Class A, Class B

Both contains some data of user. i want to display that data in a view. so my question is how we can pass multiple class data in a single view. because one view has only one model.

I don''t want to create new class for this. also m using Entity framework

推荐答案

使您的问题更清晰


当您需要多个类时,可以使用该方法是为此添加一个类:

When you need more than one class the way to do it is to add a class for that purpose:

public class TheOneModelForView
{
    public ClassA ObjectA { get; set; }
    public ClassB ObjectB { get; set; }
}



顺便说一句,一旦您的应用程序开始变得更加复杂,在某些情况下,从数据库生成的模型将无法满足您的视图需求.

您还可以对页面的不同部分使用不同的局部视图.这样,您便可以使用适当类型的对象(例如ClassA)自动为部分视图生成代码,然后在渲染视图时传递模型的该子部分.像这样的东西:



By the way as soon as your application begin to be somewhat more complex, there will be cases where models generated from the database won''t match the need for your views.

You can also uses different partial views for different part of the page. Thus, you might then be able to automatically generate the code for the partial view using an object of the appropriate type (say ClassA) and then pass that sub-part of the model when rendering the view. Something like:

Model.RenderPartial("PartialViewForA", Model.ObjectA);



如果您需要一个新的班级,那么不要让自己沉迷于现有的班级.



If you need a new class then don''t retrict yourself to existing classes.


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

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