views:

82

answers:

2

A View accepts one Model. But I need to draw HTML input controls for two models. An example will illustrate:

I have a screen where I add Employees. After adding their first name, last name and so on, I need the user to choose a number of Companies the Employees could be in.

The Companies are in one table. The Employees are in another. And a linking table joins them.

So it seems I need to pass the Companies to the View. Can I pass multiple models to the view? Or do I have to do an ugly database lookup in the View to find the Companies and manually spit out HTML for checkboxes without HTML helpers?

+5  A: 

A Model doesn't have to consist of just one object or a single collection of one type of object. It can contain many objects and/or collections of objects. It seems that the model required for your page consists of at least collections of both employees and companies. If you have no type which fits this bill in your business object abstraction then you need to create a ViewModel for this page which can do the job.

This answer may help to explain how a ViewModel fits in http://stackoverflow.com/questions/1939403/mvvm-viewmodel-vs-mvc-viewmodel/1939606#1939606

AdamRalph
This is the way I've done it in the past. Then if the view has a partial view which takes one of these "sub" viewmodels then you can pass in straight to them just by calling Model.PartialModel and it's still strongly typed :-)
WestDiscGolf
A: 

This is not entirely obvious - I'm sure it is to some guru types but for the rest of us trying to work things out its a bit more interesting.

Without going into detail I see a number of ways of solving this:

  1. You need both sets of data so you need a view specific View Model
  2. Your model is the Employee but you can still add other data to the ViewData - so make the Employee the model and pass the company data in as well as view data but not part of the model
  3. (You may want to do this regardless of what model you use) Render the company selection elements as a separate view - which is where things get interesting - you obviously have to pass the existing selection, or a means to identify same, to the component view but do you have to pass the company list or could you, at this point, cheat a bit (prgamatically)? My feeling is that this is a partial view and that you need to pass it a company selection model (list of companies with selection indicated) but you could in theory do RenderAction and have an action that returns a view that goes to get the company selection for the specified employee - that way the overall view never sees the company data, that becomes a separate encapsulated problem - at least in terms of loading the data.

I think in this case where you're adding the employee either tweaking the model or adding the company list as supplementary data to the ViewData is sufficient, but for editing - assuming its required - rather than inserting you want a company selection list (all the companies with flags to indicate which are currently selected) rather than just a list of companies and at that point it all gets a bit more interesting

Murph