Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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

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


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:

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


If you need a new class then don't retrict yourself to existing classes.
 
Share this answer
 
make your question more clear
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900