Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:21:13+00:00 2026-05-30T01:21:13+00:00

In my Controller I have a ProductInfo class from my Domain Model and I

  • 0

In my Controller I have a ProductInfo class from my Domain Model and I need some of its information to populate my View Model ProductStatsVM.

How do you populate the View Model? I heard three possible ways:

  1. Populate the View Model directly from the Controller (not good, I want to keep my Controller slim)
  2. By using a View Model constructor and pass the domain model as parameter. (I have to create a constructor for each domain model class I want to use)
  3. By using a Fill() method. (I saw it on the web, no idea how it works I guess this way the ViewModel should be aware of the Service Layer and creates coupling).

I know there are tools like AutoMapper, which I am going to use indeed, but before I want to understand the logic on how to fill a View Model from the Controller without using any additional tool.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:21:14+00:00Added an answer on May 30, 2026 at 1:21 am

    The idea is that your controller action queries some repository to fetch a domain model. Then it passes this domain model to a mapping layer which is responsible to convert it to a view model and finally it passes the view model to the view:

    public ActionResult Index(int id)
    {
        ProductInfo product = repository.GetProductInfo(id);
        ProductViewModel viewModel = Mapper.Map<ProductInfo, ProductViewModel>(product);
        return View(viewModel);
    }
    

    and you could even make your controller slimmer by introducing a custom action filter that will automatically intercept the Model in the OnActionExecuted event and call into the mapping layer to substitute it with the corresponding view model so that your controller action now becomes:

    [AutoMapTo(typeof(ProductViewModel))]
    public ActionResult Index(int id)
    {
        ProductInfo product = repository.GetProductInfo(id);
        return View(product);
    }
    

    and of course now the view is strongly typed to ProductViewModel:

    @model ProductViewModel
    ...
    

    Up to you to implement the Mapper.Map<TSource, TDest> method. And if you don’t want to implement it yourself you could download AutoMapper which already has this method for you.

    The mapping layer is something that is part of the MVC application. It must be aware of both the domain models coming from your service layer and the view models defined in your MVC application in order to be able to perform the mapping.

    Don’t use constructors (other than the default parameterless one) in your view models. The default model binder will choke if the view model doesn’t have a parameterless constructor in your POST actions and you will have to implement custom model binders.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base Controller class and all action controllers are derived from
I have a view controller that lists some data in an UITableView . To
I have controller and model. i am modifying a variable value in a model,
Have Controller: public class MyController : Controller { [HttpGet] public ActionResult MyAction(int iMode, string
I have controller PlayerController and actions inside: View , Info , List . So
I have Controller: [MySite]\Controllers\DistributionTools\TrackingChannelsController.cs [HttpPost] public void InitTcFirstPageView() { var model = new TcFirstPageModel
I have @Controller @RequestMapping(value=core/*) public class CoreController { public static String exceptionOccurredView = /core/exceptionOccurred;
I have controller my_controller and in it the action my_action . In my_action view
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
I have 3 view controller in a tab bar controller. Clicking on any tab

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.