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 6974275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:12:58+00:00 2026-05-27T17:12:58+00:00

Earlier today, a helpful person (here on Stack Overflow) pointed me towards AutoMapper ,

  • 0

Earlier today, a helpful person (here on Stack Overflow) pointed me towards AutoMapper, I checked it out, and I liked it a lot! Now however I am a little stuck.

In my Code First MVC3 Application, on my [Home/Index] I need to display the following information from my Entities:

  1. List of Posts [ int Id, string Body, int Likes, string p.User.FirstName, string p.User.LastName ]
  2. List of Tags [int Id, string Name]
  3. List of All Authors that exist on my Database [ string UrlFriendlyName ]

So far I have managed only point 1 in the list by doing the following for my Index ViewModel:

public class IndexVM
{
    public int Id { get; set; }
    public string Body { get; set; }
    public int Likes { get; set; }
    public string UserFirstName { get; set; }
    public string UserLastName { get; set; }
}

And on the Home Controller, Index ActionMethod I have:

public ActionResult Index()
{
    var Posts = postsRepository.Posts.ToList();
    Mapper.CreateMap<Post, IndexVM>();
    var IndexModel = Mapper.Map<List<Post>, List<IndexVM>>(Posts);
    return View(IndexModel);
}

Finally on my View I have it strongly typed to:

@model IEnumerable<BlogWeb.ViewModels.IndexVM>

And I am passing each Item in the IndexVM IEnumberable to a Partial View via:

@foreach (var item in Model)
{
    @Html.Partial("_PostDetails", item)
}

My question is, how can I also achieve point 2 and 3, whilst not breaking what I’ve achieved in point 1.

I tried putting the stuff I currently have for IndexVM into a SubClass, and having a List Property on the Parent class, but it didn’t work.

  • 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-27T17:12:59+00:00Added an answer on May 27, 2026 at 5:12 pm

    As an alternative to including all of your lists of entities on a single viewmodel, you could use @Html.Action. Then, in your screen view:

    @Html.Action("Index", "Posts")
    @Html.Action("Index", "Tags")
    @Html.Action("Index", "Authors")
    

    This way, your Index / Screen view & model don’t need to know about the other viewmodels. The partials are delivered by separate child action methods on separate controllers.

    All of the automapper stuff still applies, but you would still map your entities to viewmodels individually. The difference is, instead of doing the mapping in HomeController.Index(), you would do it in PostsController.Index(), TagsController.Index(), and AuthorsController.Index().

    Response to comment 1

    public class IndexVM
    {
        // need not implement anything for Posts, Tags, or Authors
    }
    

    Then, implement 3 different methods on 3 different controllers. Here is one example for the PostsController. Follow the same pattern for TagsController and AuthorsController

    // on PostsController
    public PartialViewResult Index()
    {
        var posts = postsRepository.Posts.ToList();
    
        // as mentioned, should do this in bootstrapper, not action method
        Mapper.CreateMap<Post, PostModel>();
    
        // automapper2 doesn't need source type in generic args
        var postModels = Mapper.Map<List<PostModel>>(posts); 
        return PartialView(postModels);
    }
    

    You will have to create a corresponding partial view for this, strongly-typed as @model IEnumerable<BlogWeb.ViewModels.PostModel>. In that view, put the HTML that renders the Posts UI (move from your HomeController.Index view).

    On your HomeController, just do this:

    public ActionResult Index()
    {
        return View(new IndexVM);
    }
    

    Keep your view strongly-typed on the IndexVM

    @model IEnumerable<BlogWeb.ViewModels.IndexVM>
    

    … and then get the Posts, Tags, and Authors like so:

    @Html.Action("Index", "Posts")
    

    Response to comment 2

    Bootstrapping… your Mapper.CreateMap configurations only have to happen once per app domain. This means you should do all of your CreateMap calls from Application_Start. Putting them in the controller code just creates unnecessary overhead. Sure, the maps need to be created – but not during each request.

    This also helps with unit testing. If you put all of your Mapper.CreateMap calls into a single static method, you can call that method from a unit test method as well as from Global.asax Application_Start. Then in the unit test, one method can test that your CreateMap calls are set up correctly:

    AutoMapperBootStrapper.CreateAllMaps();
    Mapper.AssertConfigurationIsValid();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to subclass threading.Condition earlier today but it didn't work out. Here is
I asked a question here earlier today and got that fixed, but now I
I asked a question earlier today here . However, what I neglected to check
I asked this question earlier today. After trying out suggestions from a few members,
It was working earlier today... :S but now, whenever I click a link in
Following a question asked here earlier today and multitudes of similary themed questions, I'm
I can't quite figure out what's going wrong. This was working earlier today and
I got some help with this earlier today but I cannot figure out the
I needed a utililty function earlier today to strip some data out of a
Earlier today a question was asked regarding input validation strategies in web apps .

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.