tags:

views:

23

answers:

2

I have having a little trouble wrapping my head around the design pattern for MVC when the data type of the model property is very different than what I wish to display in a form. I am unsure of where the logic should go.

Realizing that I am not really sure how to ask the question I think I will explain it as a concrete example.

I have a table of Invoices with a second table containing the InvoiceDetails. Each of the InvoiceDetail items has an owner who is responsible for approving the charge. A given invoice has one or more people that will eventually sign off on all the detail rows so the invoice can be approved. The website is being built to provide the approval functionality.

In the database I am storing the employee id of the person who approved the line item. This schema provides me a model with a String property for the Approved column.

However, on the website I wish to provide a CheckBox for the employee to click to indicate they approve the line item.

I guess my question is this -- how do I handle this? The Model being passed to the View has a String property but the form value being passed back to the Controller will be of the CheckBox type. I see two possible ways...

1) Create a new Model object to represent the form fields...say something like FormInvoiceDetails...and have the business logic query the database and then convert the results to the other type. Then after being submitted, the form values need to be converted back so the original Model objects can be updated.

2) Pass the original InvoiceDetails collection to the View and have code there create render the CheckBox based on the value of the String property. I am still not sure how to handle the submission since I still need to map back the form values to the underlying database object.

Maybe there is a third way if not one of these two approaches?

To make the situation a bit more complicated (or maybe it doesn't), I am rendering the form to allow for the editing of multiple rows (i.e. collection).

Thanks for any insight anybody can provide.

+1  A: 

I believe what you are looking for is the ViewModel.

In cases where you are using a ViewModel, you design the ViewModel to match the exact data you need to show on your page.

You then use your Controller to populate and map your data from your Model in to your ViewModel and back again.

The Nerd Dinner ASP.NET MVC Example has some very good examples of using ViewModels.

Justin Niessner
The examples in the Nerd Dinner, along with the examples I have seen on the web, all seem to have to do with passing additional data to the View as opposed to transforming the data to be displayed. However, your suggestion of using ViewModels seems to be the best solution. Thanks.
Jason
+1  A: 

You need a ViewModel, like @Justn Niessner suggests.

Your controller loads the complete model from the database, copies just the fields it needs into a ViewModel, and then hands the ViewModel off to the view for rendering.

I'd use Automapper to do the conversion from Model to ViewModel. It automates all the tedious thingA.x = thingY.x; code.

Here is an additional blog post going over in detail the use of ViewModels in the Nerd Dinner sample.

Ryan Michela