Marko
Marko

Reputation: 10992

How to display data from several models in the same view?

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?

Upvotes: 0

Views: 139

Answers (1)

Tahbaza
Tahbaza

Reputation: 9548

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.

Upvotes: 3

Related Questions