I am using ASP.NET MVC 3 and AutoMapper.
In my category controller I return a list of categories and I want to map each category to a category view model which is used in my grid. I have my own mapper class method that accepts the source, source type and destination type and then does the mapping of single objects. How would I add and additional method so that I can map lists?
For example, if I want to map a single category to my category edit view model then the following mapping will be used:
Mapper.CreateMap<CategoryCreateViewModel, Category>();
In my controller I would map the 2 like this:
Category category = (Category)categoryMapper.Map(viewModel, typeof(CategoryCreateViewModel), typeof(Category));
This is what my mapping method looks like:
public class CategoryMapper : ICategoryMapper
{
static CategoryMapper()
{
Mapper.CreateMap<Category, CategoryCreateViewModel>();
Mapper.CreateMap<Category, CategoryViewModel>();
Mapper.CreateMap<CategoryCreateViewModel, Category>();
}
public object Map(object source, Type sourceType, Type destinationType)
{
return Mapper.Map(source, sourceType, destinationType);
}
// I have been trying to get this right but not working
//public object Map(object source, IEnumerable<Type> sourceType, IEnumerable<Type> destinationType)
//{
// return Mapper.Map(sourceType, destinationType);
//}
}
I want to add another method where I can map lists. How would I do this?
I wanted to use my IMapper methods like I mentioned above. With the help of
Darin DimitrovI got this to work. Here is my controller code:It works with my current IMapper Map method.