2011-07-30 6 views
12

ソースとデスティネーションをマッピングした後にAutoMapperがメソッドを呼び出すのは可能ですか?ViewModelのマッピング後にAutoMapperがメソッドを呼び出す方法

私のViewModelには次のようになります。

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()); 
    } 

} 

そして、私のコントローラは次のようになります。

 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); 
    } 

私は(ソートを呼び出すAutoMapper持っていると思います)の代わりに、foreachループを行う方法、 。これは可能ですか?

答えて

18

私はあなたがここに

Mapper.CreateMap<Category, ShowCategoriesViewModel>() 
    .ForMember(dest => dest.SubCategories, opt => opt.MapFrom(origin => origin.Category1)) 
    .AfterMap((c,s) => s.Sort()); 
.AfterMap使用することができると思います
関連する問題