CiccioMiami
CiccioMiami

Reputation: 8256

How to map two different classes into one ViewModel with Automapper?

I am developing an ASP.NET MVC3 application in C# and Razor. The architecture of the application is divided in Data Access Layer (EF classes + Repository), Service Layer, Controller, ViewModels and View.

In the controller I obtain from my Service Layer:

  1. From the method Product GetProduct(id), a Product object which contains all the information about a product to be displayed in the View

  2. From the method Filter GetFilter() a Filter object that contains all the info that are used for the search filter such list of products to be chosen from a dropdownlist, etc...

Now if I use AutoMapper how can I map these information in a SelectProductViewModel class?

public class SelectProductViewModel
{
    public Product ProductToDisplay { get; set; }
    public Filter SearchFilter { get; set; }
}

Upvotes: 0

Views: 1786

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

AutoMapper is used to map between a single source type to a single destination type. You cannot use it in this context. So it could be as simple as:

var model = new SelectProductViewModel
{
    ProductToDisplay = service.GetProduct(id),
    SearchFilter = service.GetFilter()
};

Upvotes: 1

Related Questions