views:

9

answers:

1

I have a Product Model from the database which I display on the View, but if I also wanted to display Categories also and so forth. How to do that?

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcAppNorthwind.Models.Product>>" %>

But if you want to display or use data from several models in the same view? How do you do it then?

+3  A: 

Create a new class (termed a ViewModel object) whose purpose in life is to provide the data you need for your view and is not concerned with your database structure. If you're concerned about having to copy properties from one object to another look into Automapper.

Tahbaza
isn't that gonna get messy?
marko
I thought that too before I tried it and then used Automapper to eliminate the tedium. You'll find that you'll usually want to examine/validate the values passed back from your UI before populating a database/model object and you may also want to provide collection properties on your ViewModel object to be the source of your dropdownlists, both activities are clean when you employ ViewModel objects. Automapper will keep things clean in your action definitions by reducing the copy operation to to a single line in most cases.
Tahbaza
+1, Automapper is awesome.
Necros