Hi,
I have a question about passing parameters using ProjectTo<SampleViewModel>(), not to "SampleViewModel" but to nested mapped model.
I have custom mapping configuration and create all configurations in the view-models, but sometimes I need to pass parameters not only to the top view model but also to the nested ones.
Scenario:
Controller:
var userId = this.User.Identity.GetUserId();
db.ForumQuestions.ProjectTo<ForumQuestionViewModel>(new { userId });
ForumQuestionViewModel : IMapFrom<ForumQuestion>, IHaveCustomMappings
{
public string Title { get; set; }
public string Content { get; set; }
public bool IsReportedByUser { get; set;}
public IEnumerable<ForumAnswerViewModel> Answers { get; set; }
public void CreateMappings(IConfiguration configuration)
{
var userId = string.Empty;
configuration.CreateMap<ForumQuestion, ForumQuestionViewModel>()
.ForMember(m => m.IsReportedByUser, opt => opt.MapFrom(e => e.Reports.Any(r => r.ReporterId == userId)));
}
}
Let's assume that we need to know if the current user has reported this question for something.
So far so good
This works, but I want to pass parameters to answers to see if the current user has reported the answer too.
ForumAnswerViewModel : IMapFrom<Answer>, IHaveCustomMappings
{
public string Content { get; set; }
public bool IsReportedByUser { get; set;}
}