How to smooth conditional object in list in automapper

I have an Item that contains a list of Product that map to their respective ViewModel objects using AutoMapper .

In my MVC project, I have an Action method that displays an Item with the selected Product . For this, I have a ViewModel called ItemDetailsViewModel which contains a flattened Item object, a ProductViewModel list ProductViewModel and a flattened selected Product .

The difficulty I ran into is best to display this flattened selected Product .

Think of it like eBay, where you have an Item , and you can choose several options, for example. by color. For me, there are several options: Products . When the user selects Product , I want to return ItemDetails ie Item , the Products list and the selected Product .

I was wondering how to do this? ItemDetailsViewModel now, my method maps an Item to an ItemDetailsViewModel , selecting the desired ProductViewModel , and then specifically mapping each ProductViewModel property back to the ItemDetailsViewModel . In addition, due to the fact that Item and Product have the same named properties, the last line that displays the reverse product overwrites the Item ID and code.

Any suggestions on how best to set up a mapping?

I missed the display that I have in place, since it is basically a direct one-to-one mapping, except for displaying the selected ProductViewModel back to ItemDetailsViewModel .

 Mapper.CreateMap<Item, ItemViewModel>() .ReverseMap(); Mapper.CreateMap<ProductViewModel, ItemDetailsViewModel>() .ForMember(d => d.ProductId, o => o.MapFrom(s => s.Id)) .ForMember(d => d.ProductCode, o => o.MapFrom(s => s.Code)); 

Classes

 public class Item { public int Id { get; set; } public string Code { get; set; } public IList<Product> Products { get; set; } } public class Product { public int Id { get; set; } public string Code { get; set; } } public class ItemViewModel { public int Id { get; set; } public string Code { get; set; } public IList<ProductViewModel> Products { get; set; } } public class ItemDetailsViewModel : ItemViewModel { public int ProductId; public string ProductCode; } public class ProductViewModel { public int Id { get; set; } public string Code { get; set; } } 

Act

 public ActionResult ItemDetails() { var item = new Item { Id = 1, Code = "Item1", Products = new List<Product>() { new Product { Id = 1, Code = "Product1" }, new Product { Id = 2, Code = "Product2" }, new Product { Id = 3, Code = "Product3" }, } }; var productCode = "Product2"; var itemDetailsViewModel = Mapper.Map<ItemDetailsViewModel>(item); if (itemDetailsViewModel.Products != null && itemDetailsViewModel.Products.Count > 0) { ProductViewModel productViewModel = null; if (!String.IsNullOrEmpty(productCode)) productViewModel = itemViewModel.Products.FirstOrDefault(e => e.Code.Equals(productCode, StringComparison.OrdinalIgnoreCase)); if (productViewModel == null) productViewModel = itemViewModel.Products[0]; Mapper.Map<ProductViewModel, ItemDetailsViewModel>(productViewModel, itemDetailsViewModel); } } 
+8
c # automapper automapper-3
source share
1 answer

One solution would be to ignore the properties in the ItemDetailsViewModel in your display from ProductViewModel to ItemDetailsViewModel :

 Mapper.CreateMap<ProductViewModel, ItemDetailsViewModel>() .ForMember(d => d.Id, opt => opt.Ignore()) .ForMember(d => d.Code, opt => opt.Ignore()) .ForMember(d => d.ProductId, o => o.MapFrom(s => s.Id)) .ForMember(d => d.ProductCode, o => o.MapFrom(s => s.Code)); 
0
source share

All Articles