c
Umb 10.6.1 In a route hijacking scenario I want to map a load of children into what is the same model but with a different name. i.e. all SingleEventItem into a list of EventItem. Using this code I don't understand why it doesn't work. Mainly because I don't believe it the error. I'll be adding other items to the list later, but probably in a more manual way.
Copy code
List<EventItem> eventList = new List<EventItem>();
  // Get all Single Event Items
  List<SingleEventItem> singleEventList = CurrentPage.Children<SingleEventItem>().ToList();
  eventList = _umbracoMapper.Map(singleEventList, eventList);
The error is:- ``InvalidOperationException: Don't know how to map System.Collections.Generic.List`1[[Umbraco.Cms.Web.Common.PublishedModels.SingleEventItem, Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.List`1[[Umbraco.Cms.Web.Common.PublishedModels.EventItem, Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].`` It's my first time mapping stuff and I've probably got it horribly wrong, so any advice would be appreciated. Thanks.
r
Hey, have you defined the mapping definition? https://docs.umbraco.com/umbraco-cms/reference/mapping#defining-mappings It should accept a source of event list and a target of single event list *just realised the docs were original sent for v12 however v10 shares the same implementation *
c
Yes, or at least I think I have. See the eventLIst and singleEventList code in the original post.
Copy code
List<EventItem> eventList = new List<EventItem>();
  List<SingleEventItem> singleEventList = CurrentPage.Children<SingleEventItem>().ToList();
Gave up in the end. Mapped it long-hand.
m
_mapper.MapEnumerable needs to be used for lists and other enumerables, assuming you had a map definition for EventItem to SingleEventItem
c
EventItem and SingleEventItem were exactly the same definition. Hadn't seen MapEnumerable anywhere. So are you saying it should have been
Copy code
eventList = _umbracoMapper.MapEnumerable(singleEventList, eventList);
?
m
var eventList = _umbracoMapper.MapEnumerable(singleEventList); https://docs.umbraco.com/umbraco-cms/reference/mapping#mapping
a
When you get the "Dont know how to map " Have you registered the mapper in the composer/umbracobuilder ? 🙂
c
The docs aren't the ones I was looking at earlier. But I'll revisit this next time I have a perceived need to map. I solved my mapping problem with 8 short lines of code in 5 mins without using a "mapper". I suspect my use case wasn't sufficiently complex to warrant the effort of setting up a mapper in the first place. I just thought I'd give it a go as a learning experience.
2 Views