Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • cheil 10 posts 90 karma points
    Sep 14, 2016 @ 01:41
    cheil
    0

    Using Ditto on Nested Content with custom data models

    I am trying to use ditto to map a list of items to a property on a document type using nested content. I don't want to use IPublishedContent in my PageModel so I can have strongly typed models to pass to the presentation layer.

    The simple example I have created is a document type called Page, a property called TestList, which is of type nested content a list of items that are a document type called TestItem. TestItem is a simple testbox.

    I have a similar structure in code that follows this pattern:

    Page = class "PageModel", property "TestList" type "TestListModel" TestList = class "TestModelList", property "Content" type "List

    I have tried decorating everything with [UmbracoPicker], I tried using virtual IEnumerable

    But no matter what i try I am getting this error

    Unable to cast object of type '<CastIterator>d__94`1[ProjectName.TestItem]' to type 'System.Collections.Generic.List`1[ProjectName.TestItem]'.
    

    It seems like a simple task, Ditto already converts IPublishedContent to custom models, nested content created a list of IPublishedContent, but I can't seem to get Ditto to convert a list of IPublishedContent into a list of Models.

    Can anyone point me in the right direction?

    Thanks, Chris

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Sep 14, 2016 @ 08:18
    Lee Kelleher
    0

    Hi Chris,

    Would you be able to copy/paste in your PageModel class? (It'll give me a better understanding of the processors/attributes being used)

    At a complete guess, try changing List<TestItem> to IEnumerable<TestItem>?

    Cheers,
    - Lee

  • cheil 10 posts 90 karma points
    Sep 14, 2016 @ 15:55
    cheil
    0

    Edit: Turns out all i needed was a night of sleep and walking through all the steps. I removed [UmbracoPicker] from the TestItem class and it worked. Thanks for taking a look at this for me though!

    I'll keep everything below because it might help someone else realize they have unecessary [UmbracoPicker] attributes on their classes

    This has gone through a few iterations so it looks a little different now, I got to the point where using the middle class TestModelList throws the same error as doing a direct IEnumerable

    public class PageController : RenderMvcController
    {
        public override ActionResult Index(RenderModel model)
        {
            var test = model.As<PageModel>();
            return base.Index(model);
        }
    }
    
    //Document type class model that is used by nested content
    //doc type alias = "testItem"
    [UmbracoPicker]
    public class TestItem
    {
        //property alias = "testProperty"
        [UmbracoPicker]
        public string TestProperty { get; set; }
    }
    
    //The "page" document type
    public class PageModel
    {
        //property alias = "testList"
        public IEnumerable<TestItem> TestList { get; set; }
    }
    

    The error I am getting is different now too. When I try to call test.TestList.Count() I get this error

    Can't convert IPublishedContent to System.String as it has no valid constructor. A valid constructor is either an empty one, or one accepting a single IPublishedContent parameter.
    
  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Sep 14, 2016 @ 16:17
    Lee Kelleher
    100

    Hi Chris,

    Try removing the [UmbracoPicker] attributes, as they are only needed for content/media pickers.

    Cheers,
    - Lee

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies