2

I have one view page (MyView.aspx) and many data sources to bind on this page.

Lets say it has Books, Publishers, Comments, etc.

Each one of those has an object which provides a List, List, etc.

In my view, what are my optiosn for multilple model biding?

I want to check to see if each one is empty, and then enumerate it. But I can't check Model.Count() because wouldn't Model be made of all those objects if I set the page to inheriet from ?

What are my options? Should I load each content area in a control/partial view?

Or can I just dump each object into ViewData and then check the count by casting in the view?

Thanks so much for taking a look at my problem.

2 Answers 2

6

Have you considered using a ViewModel that contains all Lists of all of your different data fields and using that to populate your View?

Example:

ViewModel:

public class MyViewModel
{
   List<Book> Books {get; set;}
   List<Publisher> Publishers {get; set;}
   List<Comment> Comments {get; set;}
   //Other fields...

   //Constructors...
}

Then in your View you could simply check if a specific field was null prior to enumerating through it:

View:

if(Model.Books.Count() != 0)
{
   //Enumerate through results here
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, that is a great idea. I keep forgetting about ViewModels. My team wants us to use concrete objects as much as possible (the Publisher object, etc). Would this be preffered to creating a partial view for each model?
You should be able to implement it just like above - and just pass all of the object into a single view. However - if you are going to be just displaying a single type of data in a view, then just passing in a collection of the items might be your best bet.
Much appriciated. Thanks for your help!
0

Rionmonster's is probably the best solution - another is to use strongly typed partial views. You could load everything into the view data and then inject each segment into the respective partial view.

2 Comments

I agree, create a model to hold each object's list of objects is a good method. I am considering strongly typed parial views, as it would allow me to bind directly to the concrete objects (Publisher, etc). Also, if I wanted to convert the content areas to load via AJAX, I would need parital views to do so... Does that sound correct?
Breaking them into partial views will definitely ease your Ajax life since you can just return the partial as your ajax response, thus only reloading the bits that you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.