c# - ViewModel with Collection of another ViewModel - Can AutoMapper Help Me Here? -


itemprop = "text">

I see a ViewModel that looks like this:

  public class CreateReviewViewModel {public string Title {received; Set; } Public Decimal Score {get; Set; } Public Iconization & lt; Recommended Visual Circle & gt; Get recommendations { Set; Therefore, the first two basic basic types are - Easy.  

The third asset is a collection of other view models:

  public class Recommend ViewModel {Public Recommendation Type Recommended Type {get; Set; } Public Bull isRecommendedFor {get; Set; }}   

Recommendation type In my domain model there is a enum , which is byte the price is different " Recommendation "

On my [HttpGet] action, I do not do this.

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

So I end up with RecommendationViewModel , set to bool properties false . ;

@ Html.EditorFor (model.Recommendations Model = & gt):

Then on my view, I EditorTemplates Uses

which calls a custom editor template that returns a label and checkbox for two properties. Cool

So - the background, which is hopeful that makes sense.

I review domain model to [hppist] action?

review part of the object looks like this:

  public class review {public bool isRecommendedForA {received; Set; } Public Bull IERIdFor B {Receive; Set; } // etc}   

I am currently custom mapping in this way:

  var review = new review (); Review. It is recommended FORA = this. Singleor Default (x => x. Irrational Type == Recommendation Type A) IRecommendedFor; Review. Recommended Forb = This. Single default (x => X. Recommendation type == Recommendation type B). For analogue;   

Which is very tedious.

Can I do it with Automaker?

Of course, I have the basic properties on ViewModel instead of a collection of all the different RecommendationType, but then my view becomes complicated, and I get the EditorTemplates Can not use to loop indirect collections - I would write Html.EditorFor

Any ideas?

I have one possible solution :

  Mapper.CreateMap & LT; CreateReviewViewModel, review & gt; () .Member (destination = & gt; dest.IsRecommendedForA, opt => opt.MapFrom (src = & gt; src.IsRecommendedFor (RecommendationType.A)) .forMember (destination = & gt; dest.IsRecommendedForB, Opt => opt.MapFrom (src = & gt; src.IsRecommendedFor (RecommendationType.B)));   

Where there is a hook property using LINQ expression first, where there is an asset in the model to work, and it has been checked.

P>

Manual is better than left to right, but still not good.

I have taken a look at custom converters / revolvers, but do not take any extra parameters (for example, by using this parameter), so I can not see that I use them How can i

I will go with this time, but I'm hoping someone knows how to do it better.

Comments