AronChan
AronChan

Reputation: 245

How do i access data from multiple models/controllers in my view?

I'm trying to create a view that will create a subcategory.

In order to create this view I inherit my model for subcategories in the view in order to access the required attributes etc.

Now one of the attributes I wish to set on my subcategory object is a reference to a normal Category.

However I am not sure how to populate my dropdownlist with category items since I am already using the model of subcategories.

My question is, what would be the best way to access a SelectList of category objects in my subcategory view.

I've considered using partial views or perhaps transfering the data in my ViewBag or ViewData.

What would be the best way to do this?

Upvotes: 0

Views: 118

Answers (1)

Jamie Dixon
Jamie Dixon

Reputation: 53989

You need to use a ViewModel. This is basically a class that you use to bring together all the pieces of the various models required for your view.

It might look something like:

public class CategoryListingsVieWModel
{
   public IList<ISubCategory> Subcategories{get;set;}
   public IList<ICategory> Categories{get;set;}
   [...] // Any other data your view needs
}

This then becomes the model for your view which your pass from your controller.

Upvotes: 4

Related Questions