0

Consider the following code snippet

public class FirstViewModel
{
    public IEnumerable<SelectListItem> GetSomeData()
    {
      //query dbcontext here
    }
}

public class SecondViewModel
{
    public Employee Employee { get; set; }
    public Stock Stock { get; set; }
    //more properties
}

So with the first snippet I can do the following in my code:

@Html.DropDownListFor(model => Model.Employee, new App.Models.ViewModels.FirstViewModel().GetSomeData(), "Please choose something")

In the second snippet I can do:

@Html.DropDownListFor(model => Model.Employee, new SelectList(Model.Employee), "Select Status")

What I'm trying to achieve is to have multiple models in one view for my MVC app. My question is would it be better to have a separate view model for each or one big view model which reference all other models. I'm very new to MVC so any help would be appreciated.

Izzy
  • 6,740
  • 7
  • 40
  • 84
  • typically you would use a view model to pass in multiple objects, so I would say a "big view model" – Harry Mar 29 '16 at 11:29
  • @Harry So I've got a view which I need to pass in 5 different object and you're suggestion to have one big viewmodel and pass each object as required? – Izzy Mar 29 '16 at 11:30
  • well, that completely depends on your requirement. separate views are advisable though! – Sunny Sharma Mar 29 '16 at 11:32
  • as mentioned in comments, it depends on use. have you considered PartialViews within your view each PartialView can have its own model – Harry Mar 29 '16 at 11:35
  • @Harry That's the route I'm planning on taking to use `PartialViews` – Izzy Mar 29 '16 at 11:36
  • Possible duplicate of [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) – rogerdeuce Mar 29 '16 at 13:23

2 Answers2

0

Each of my Views has a ViewModel - that may be an aggregate of two VMs (as properties). Important to remember the VM is a Model for the View - its not a Data Model in its own right. The data model should be elsewhere and the Controller maps between the two.

(I've never had a ViewModel actively pull data from a DB before - I've always had the Controller populate the ViewModel. Not directly related to your question, but important to go in the right direction from the start.)

PhillipH
  • 6,182
  • 1
  • 15
  • 25
  • Would the same apply to a partial view too? – Izzy Mar 29 '16 at 11:35
  • Yes - I create a ViewModel for each Partial. That means I can reuse the Partial and its VM elsewhere. The "Parent" View has a "Parent" VM which includes a reference to the Partial VM as a property. – PhillipH Mar 29 '16 at 11:38
  • Would it be possible if you can provide a small code example please – Izzy Mar 29 '16 at 11:39
0

I would base it on use. If you need all of the data all of the time why not include it all.

Other things to consider:

  1. Do you need all of the data all the time?
  2. Are there maximums to worry about for reads?
  3. Possible memory limitations?
  4. Number of visits/traffic load considerations.