How to make AutoMapper call a method after mapping a ViewModel

Viewed 10661

Is it possbile to make AutoMapper call a method after mapping source and destination?

My ViewModel looks like this:

 public class ShowCategoriesViewModel
{
    public int category_id { get; set; }
    public string category_name { get; set; }

    public List<MvcApplication3.Models.Category> SubCategories { get; set; }

    public void Sort()
    {
        SubCategories.Sort(new CompareCategory());
    }

}

And my Controller looks like this:

        public ActionResult Index()
    {
        var category = db.Category.Where(y => y.parrent_id == null).ToList();

        Mapper.CreateMap<Category, ShowCategoriesViewModel>().
            ForMember(dest => dest.SubCategories, opt => opt.MapFrom(origin => origin.Category1));

        List<ShowCategoriesViewModel> scvm = Mapper.Map<List<Category>, List<ShowCategoriesViewModel>>(category);

        foreach (ShowCategoriesViewModel model in scvm)
        {
            model.Sort();
        }

        return View(scvm);
    }

I would like to have AutoMapper call the Sort() method, instead of doing a foreach loop. Is this possible?

1 Answers
Related