Copied to clipboard

Flag this post as spam?

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


  • Peter 89 posts 160 karma points
    Apr 18, 2013 @ 15:18
    Peter
    0

    Umb 6 Custom Model - The model item passed into the dictionary is of type.... requires Umbraco.Web.Models.RenderModel

    Hey y'all, 

    I'm developing locally with the UmbracoCms.6.0.3 release.

    I am hoping someone can point me in the right direction. I have had a look around Our and googled about it too, but just can't seem to join the dots. I can set up a custom model, but having a template use that model AND have a layout to sit it fails. :-/

    I've followed http://our.umbraco.org/Documentation/Reference/Mvc/custom-controllers but I must be missing something.. I'll replace the name used for the template/controller/model, with Custom

    I have a template to display the item which is used by an item in the cms - simplistiy they are named the same thing  so  http://dev.website.local/custom is the name of the page in the cms and "custom" is the name of the template - and by debugging I know this page is being hit.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<SiteNamespace.UmbracoMVC.Models.CustomModel>
    @{
        Layout = "_layout.cshtml";
    }
    <div>webpage with using the custom model</div>
    <div>@Model.CustomItem</div>

    I have a layout page (_layout.cshtml) which the template should be using.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    <!DOCTYPE html>
    _.... blah blah blah..._
    @RenderBody()
    _.... blah blah blah.._
    </html>

    A custom controler 

    public class CustomController : Umbraco.Web.Mvc.RenderMvcController
        {
            public override ActionResult Index(RenderModel model)
            {
                //Do some stuff here, then return the base method
                //return base.Index(model);
    
                var customModel = new CustomModel()
                    {
                        things = ThingService.ReadAll(),
                    };
    
                return CurrentTemplate(customModel);
            }
        }

    But this gives me 

    The model item passed into the dictionary is of type 'SiteNamespace.UmbracoMVC.Models.CustomModel', but this dictionary requires a model item of type 'Umbraco.Web.Models.RenderModel'.

    If I change the template to not reference the shared _layout, it has no issues.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<SiteNamespace.UmbracoMVC.Models.CustomModel>
    @{
        Layout = null;
    }
    <div>webpage with using the custom model</div>
    <div>@Model.CustomItem</div>

    So, any ideas what I am missing here? Probably something obvious.

    Thanks!

  • Ravi Motha 289 posts 499 karma points MVP 5x c-trib
    Apr 18, 2013 @ 15:25
    Ravi Motha
    0

    have you tried using it with another layout to see what the result is. maybe you have a duff reference to something???

  • Peter 89 posts 160 karma points
    Apr 18, 2013 @ 15:32
    Peter
    0

    Hey Rav! You know what, I didn't think I had anything complex or interesting going on in the _layout ... so hadn't tried that.

    No difference :-/

    Thanks though. :)

     

     

  • Dirk De Grave 4539 posts 6013 karma points MVP 3x admin c-trib
    Apr 18, 2013 @ 15:35
    Dirk De Grave
    104

    Hi Peter,

    Derive your CustomModel from RenderModel and all will be solved... Example:

    public class CustomModel : RenderModel {
    public CustomModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) { }
    public CustomModel(IPublishedContent content, CultureInfo culture) : base(content, culture) { }
    public CustomModel(IPublishedContent content) : base(content) { }
    }

    Cheers,

    /Dirk

  • Matt Speakman 11 posts 43 karma points
    Jul 24, 2015 @ 16:53
    Matt Speakman
    0

    Works perfectly thanks.

  • Stephen 46 posts 248 karma points
    Apr 18, 2013 @ 15:36
    Stephen
    0

    See this thread: http://our.umbraco.org/forum/templating/templates-and-document-types/38311-Using-RenderMvcController which looks to be the same issue and has various solutions.

    You'll need to make sure your layout/view models inherit from each other (and have @model dynamic on your base layout if appropriate)

     

    Stephen

  • Peter 89 posts 160 karma points
    Apr 18, 2013 @ 15:46
    Peter
    0

    Thanks guys!

    Makes sense Dirk, you deserve more than a High Five.... a grade A man-hug.

    Ta!

  • Funka! 398 posts 660 karma points
    Jun 19, 2013 @ 21:10
    Funka!
    0

    If we are not able (nor want) to change our custom controllers to inherit from RenderMvcController and to change our custom models to inherit from RenderModel, ... is there any other way to do this?

    If not, is this a technical limitation of how Umbraco.Web.Mvc.UmbracoViewPage<T> works?  Or is it more fundamental. Without understanding how this is built (obviously), I wonder why it can't figure out on its own the current context and require me to modify existing MVC apps just so I can get access to some Umbraco helpers.

    My situation is that I have a website that I want to be 90% Umbraco, with lovely things like a master layout and macros/partials to generate navigation bars, sidebars, and a footer, etc.  And then, I have a custom MVC app that I simply want to be able to generate views that use this same master layout which is able to generate the same navigation bars, sidebars, and footer, etc.  However, it seems I have to completely revamp my existing MVC app's controllers and models or else the views won't work? I don't understand why this shouldn't be possible.

    Thank you for any help or guidance!

  • David Court 6 posts 26 karma points
    Apr 30, 2014 @ 18:47
    David Court
    0

    Thanks Dirk

  • Stuart 11 posts 34 karma points
    Feb 26, 2015 @ 23:47
    Stuart
    0

    I just inherited from RenderModel, thanks very much Dirk!!!

  • Luyolo Mgodi 71 posts 120 karma points
    Aug 29, 2015 @ 12:43
    Luyolo Mgodi
    0

    Hi guys,

    I'm also having the same problem, but something a little different with me is that, I am hijacking the route because I need to get data from a custom table. Hence the UmbracoContext is always null and it breaks when I try to initialize my custom model.

    Please help.

Please Sign in or register to post replies

Write your reply to:

Draft