Copied to clipboard

Flag this post as spam?

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


  • James Meredith 18 posts 91 karma points
    1 week ago
    James Meredith
    0

    Using RenderMvcController and getting error "Cannot bind source content type"

    Hi all,

    I'm trying to setup a RenderMvcController in v8.6.3 (Something I've done in V7 many times) and getting the following error "Cannot bind source content type Umbraco.Web.PublishedModels.EventPage to model type Example.Models.EventPageViewModel.". I have followed this https://our.umbraco.com/Documentation/Reference/Routing/custom-controllers trying both the "PublishedContentWrapped" and the model builder method which gives the error above. Please find my code below:-

    Controller

    using System;
    using System.Web.Mvc;
    using Example.Models;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web;
    using Umbraco.Web.Models;
    using Umbraco.Web.Mvc;
    
    namespace Example.Controllers.RenderMvc
    {
        public class EventPageController : RenderMvcController
        {
            public override ActionResult Index(ContentModel model)
            {
                var pageModel = new EventPageViewModel(model.Content);
                pageModel.Testing = "Test";
    
                return CurrentTemplate(model);
            }
        }
    }
    

    Model

    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web.PublishedModels;
    
    namespace Example.Models
    {
        public class EventPageViewModel : EventPage
        {
            public EventPageViewModel(IPublishedContent content) : base(content) { }
    
            public string Testing { get; set; }
        }
    }
    

    View

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ExampleProject.Models.EventPageViewModel>
    @{
        Layout = "master.cshtml";
    }
    
    <p>@Model.Testing</p>
    

    Any help would be much appreciated feels like I'm going round in circles

    Cheers, James

  • Marc Goodson 1246 posts 8422 karma points MVP 5x c-trib
    1 week ago
    Marc Goodson
    100

    Hi James

    Would returning the 'PageModel' to the CurrentTemplate make all the difference here?

    eg:

    namespace Example.Controllers.RenderMvc
    {
        public class EventPageController : RenderMvcController
        {
            public override ActionResult Index(ContentModel model)
            {
                var pageModel = new EventPageViewModel(model.Content);
                pageModel.Testing = "Test";
    
                return CurrentTemplate(pageModel);
            }
        }
    }
    

    Looks like your View is expecting a model of type EventPageViewModel but you are sending it a model of type 'ContentModel' :-P

    regards

    Marc

  • James Meredith 18 posts 91 karma points
    1 week ago
    James Meredith
    0

    Ouch, yep you're spot on. I would say the heat got me but the air conditioning is on :-P.

    Thanks a lot, James

  • Marc Goodson 1246 posts 8422 karma points MVP 5x c-trib
    1 week ago
    Marc Goodson
    0

    hehe

    if it's any consolation, when running the official training courses... at least a quarter of the class would do this each time in the Route Hijacking exercise...

    so my radar is still slightly attuned to spotting it!

    and it's a dead easy thing to muck up from time to time... (in V7 and V8)

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft