Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
we know that we can use multiple model in single view

please go through this link below:

Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^]

now my question is
i have 3 models
public class category
public int CatId { get; set; }
public string CatName { get; set; }

public class subcategory
public int subcatId { get; set; }
public string catid { get; set; }
public string subcatname { get; set; }

public class item
public string itemid { get; set; }
public int catid { get; set; }
public string subcatid { get; set; }
public string item { get; set; }

now i want to show
in grid(table structure) in single view

how can we show it in single view with multiple model??
Updated 31-May-15 0:14am

public class MyViewModel
    public item Item { get; set; }
    public category Category { get; set; }
    public subcategory Subcategory { get; set; }
Share this answer
utm 31-May-15 7:59am    
thank u.
from above code .. that means whenever i create new model and add it MyViewModel the i will get all i correct sir?
can u give some code sample.. to access (or fill) item model properties,category model properties etc using MyViewModel.
Create a new VeiwModel, add the properties that you require to send to your view,
make query to populate the properties from the three relations, and send the ViewModel to the view.

From the view when you need to return the values, do the reverse way, i.e. map each returned field to each your relation properties and save the data.

Hope this will help.
Share this answer
utm 31-May-15 5:21am    
Thank u for ur reply..
Now if no of table is big..then each time i have to create third model as per requirement. so it going to tuff work. how to make it simple?
if i keep all properties in one model then is it will be right approach?
right now i am not using entity framework.?but want keep same structure like entity frame work? please guide me..
Mostafa Asaduzzaman 31-May-15 5:46am    
are you using mvc or web forms?
If you use mvc, then models and viewmodels can be used for communicating between views and controller
if you need some sample let me know
utm 31-May-15 7:44am    
yes,i am using mvc framework 4.5
i am very thankful if provide me some sample.
thank u.
Mostafa Asaduzzaman 31-May-15 16:27pm    
You have three classes and you would like to make a viewmodel of these 3 classes - its easy.
In the viewmodel define the properties that you need to filter the properties from the three class and you can add extra properties for your purpose such as linking to the classes.
Example might be as:
public class ItemCategoryViewModel
public item Item { get; set; } //Item class - your definition
public category Category { get; set; } //Category class - your definition
public subcategory Subcategory { get; set; } //SubCategory class - your definition
//add new properties here

public class ItemCategory
public string CategoryName { get; set; }
public string SubCategoryName { get; set; }
public string ItemName { get; set; }
//other properties if you need
public List<itemcategory> ItemCategories { get; set; } //Enumerable definition


after defining this, you can use the viewmodel in your controller methods and views for working with the data.
When you call the viewmodel, instanciate it first.For example
var itemcategoy = new ItemCategoryViewModel.ItemCategory
and then
itemcategoy.CategoryName = [get the categoryname from the database from your category table];
itemcategoy.SubCategoyName = [get the subcategoryname from the database from your sum category table];
itemcategoy.ItemName =[get the ItemName from the database from your Item table];

after that add the information to the list to display

this will give you the Enumerable list of records for your view.

Then in the view you can loop through and display.

@foreach(var item in Model.ItemCategories)

< td >

Note: Don't forget to include @model <yournamespace>.ItemCategoryViewModel at the top of your view
utm 1-Jun-15 1:19am    
Thank very much sir,
I got ur point.
it will be very usefull for me.
thanks u sir.

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