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

  • Home
  • SEARCH
  • 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 4231766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:00:38+00:00 2026-05-21T02:00:38+00:00

I have a ViewModel that looks like this: public class CreateReviewViewModel { public string

  • 0

I have a ViewModel that looks like this:

public class CreateReviewViewModel
{
   public string Title { get; set; }
   public decimal Score { get; set; }
   public ICollection<RecommendationViewModel> Recommendations { get; set; }
}

So, the first two are basic native types – easy.

The third property is a collection of another ViewModel:

public class RecommendationViewModel
{
   public RecommendationType RecommendationType { get; set; }
   public bool IsRecommendedFor { get; set; }
}

RecommendationType is an enum in my domain model, which has byte values representing a different “recommendation”.

On my [HttpGet] action, i do this:

var model = new CreateReviewViewModel
{
   Recommendations = SomeMethodWhichLoopsThroughTheEnumMembersAndCreatesTheModel();
}
return View(model);

So i end up with a list of RecommendationViewModel, with the bool properties set to false.

Then on my View, i use EditorTemplates:

@Html.EditorFor(model => model.Recommendations)

Which calls a custom Editor template that renders a label and checkbox for the two properties. Cool.

So – there’s the background, which hopefully makes sense.

How do i map that ViewModel to a Review domain model in the [HttpPost] action?

The part of the Review object looks like this:

public class Review
{
   public bool IsRecommendedForA { get; set; }
   public bool IsRecommendedForB { get; set; }
   // etc
}

I’m currently doing custom mapping like this:

var review = new Review();
review.IsRecommendedForA = this.Recommendations.SingleOrDefault(x => x.RecommendationType == RecommendationType.A).IsRecommendedFor;
review.IsRecommendedForB = this.Recommendations.SingleOrDefault(x => x.RecommendationType == RecommendationType.B).IsRecommendedFor;

Which is very tedious.

Can i do the above with AutoMapper?

Of course, i could just add all the different RecommendationType’s as basic properties on the ViewModel instead of a collection, but then my View’s become complicated, and i can’t use EditorTemplates to implicitly loop through the collection – i would have to write out Html.EditorFor for each property.

Any ideas?

  • 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-21T02:00:39+00:00Added an answer on May 21, 2026 at 2:00 am

    I have one possible solution:

    Mapper.CreateMap<CreateReviewViewModel, Review>()
       .ForMember(dest => dest.IsRecommendedForA, opt => opt.MapFrom(src => src.IsRecommendedFor(RecommendationType.A)))
       .ForMember(dest => dest.IsRecommendedForB, opt => opt.MapFrom(src => src.IsRecommendedFor(RecommendationType.B)));
    

    Where IsRecommendedFor is a hook property, using the LINQ expression stated earlier in my question to work out if the model contains the property, and it’s checked.

    Which is better than the manual left-to-right, but still not great.

    I’ve taken a look at custom converters/resolvers, but none take additional parameters (e.g resolve from this to this, using this parameter also), so i can’t see how i can use them.

    I’ll go with this for now, but hoping someone knows how to do it better.

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

Sidebar

Related Questions

Given that I have the following widget (and a view attached to it): public
I have a TextBox.TextProperty bound to a ViewModel property. In the binding I have
Let's pretend I have the following xaml... <UserControl.Resources> <local:ViewModel x:Name=viewModel /> <local:LoadChildrenValueConverter x:Name=valueConverter />
Maybe that's not exactly the solution i need, but this is what i want
I have a WPF app which, when it starts, looks at the file system
I have a ListBox which is populated from a collection of ViewModels, which uses
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I

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.