Copied to clipboard

Flag this post as spam?

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


  • Barry Fogarty 493 posts 1129 karma points
    Dec 09, 2014 @ 13:29
    Barry Fogarty
    0

    Passing MasterModel<Interface> to a view

    If this was possible within the Hybrid Framework it would be one step closer to Umbraco development nirvana..

    Bit of background - I have the framework building against v7.2 and I am using DocType compositions for my site structure.  Stephan's amazing ModelsBuilder is doing a great job creating interfaces for these compositions and ensuring doctypes which include them implement the interface.  For example, I have a composition, 'Sidebar' which contains a widget picker.  This composition is included on the 'Home' doctype (among others) and therefore the Home generated class implements ISidebar.  So far so good.

    What's not working is passing this interface or generated class as the type of MasterModel to the View, e.g. 

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IMasterModel<Sidebar>>


    This results in an error like: The model item passed into the dictionary is of type MasterModel'[Home], but this dictionary requires a model item of type MasterModel'[Sidebar].

    Do you think this is achievable?  I have worked around the issue by creating a property on the MasterModel but it would be SUPER AWESOME if this wasnt necessary.

    EDIT: I ensured MasterModel also implemented the interface, but no joy.  I'm not that familiar with using interfaces so I'm hopefully overlooking somehting.

    public class MasterModel : RenderModel, IMasterModel 
        where T : class, ISidebar, IPublishedContent
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Dec 09, 2014 @ 13:49
    Jeroen Breuer
    0

    I did some experimenting with DocType compositions myself. Can't you just pass the model that inherits all those interfaces? Why would you only want to pass an interface? 

  • Barry Fogarty 493 posts 1129 karma points
    Dec 09, 2014 @ 15:21
    Barry Fogarty
    0

    Ah yes, forgot that bit.  Because my layout view structure is Master - Sidebar - Home.  Sidebar will be the layout for multiple views, so I was hoping it would work in a similar way that you can pass an interface or class to Model.Content.Children<T>() to filter the results.

    I guess I am just being a bit lazy and I should implement a SidebarModel that can be included/inherited in the top-level classes that need it - I was just wondering if it could be done with the structure provided by the ModelsBuilder alone.  Or just get property values the old fashioned way in that particular layout (boo!)

    Keep up the great work!  Umbraco 7.2 + Hybrid Framework rocks

  • Matt Taylor 873 posts 2086 karma points
    Feb 13, 2015 @ 11:43
    Matt Taylor
    1

    I'm struggling with this in my head too.

  • marcelh 171 posts 471 karma points
    Jun 18, 2015 @ 18:36
    marcelh
    0

    Ran into this today. Did anyone find some sort of solution?

    In my case, I've found three occasions when this issue occurs (in the most annoying order ;-)): 1) When I want to pass the model to a partial view that is included on various templates 2) When I need to access model properties in a template's parent template (using layout = ...) 3) When I want to document type to be rendered using an alternate template that can also be used got another document type.

    It must be somehow possible to let the view be a little less restrictive on the model that's passed into it ;-)

  • Maxi 13 posts 152 karma points
    Jul 07, 2015 @ 11:44
    Maxi
    0

    Hi,

    I asked a similar question, where I wanted to convert different nodes which all use composition "FeaturedImage". However, I still haven't heard of a way to do this but it might be included in the next update for Hybrid.

    The best way I have found for dealing with this situation is to use a partial view, creating a surface controller, where I create a custom model and send this to the partial view.

    Top of Partial:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<BookPopupPartial>
    

    the custom Model:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core.Models;
    using Umbraco.Extensions.Models.Generated;
    
    namespace Umbraco.Extensions.Models.Custom
    {
        public class BookPopupPartial
        {
            public Image BackgroundImage { get; set; }
            public String Title { get; set; }
            public IHtmlString Terms { get; set; }
            public UrlPicker.Umbraco.Models.UrlPicker TicketLink { get; set; }
            public IPublishedContent TermsLink { get; set; }
        }
    }
    

    and finally the SurfaceRenderMvcController:

     using System.Web;
    using System.Web.Mvc;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Extensions.Controllers.Base;
    using Umbraco.Extensions.BLL;
    using Umbraco.Extensions.Enums;
    using Umbraco.Extensions.Models;
    using Umbraco.Extensions.Models.Custom;
    using Umbraco.Extensions.Models.Generated;
    using Umbraco.Extensions.Utilities;
    using Umbraco.Web;
    
    namespace Umbraco.Extensions.Controllers
    {
        public class BookPopupController : SurfaceRenderMvcController
        {
            [ChildActionOnly]
            public ActionResult GetBookPopup()
            {
                var model = new BookPopupPartial()
                {
                    BackgroundImage = CurrentPage.GetPropertyValue<Image>("image"),
                    Title =  CurrentPage.GetPropertyValue<String>("title"),
                    Terms =  CurrentPage.GetPropertyValue<IHtmlString>("terms"),
                    TicketLink = CurrentPage.GetPropertyValue<UrlPicker.Umbraco.Models.UrlPicker>("ticketLink"),
                    TermsLink = CurrentPage.GetPropertyValue<IPublishedContent>("termLink"),
                };
    
                return PartialView("book-popup", model);
    
            }
        }
    }
    

    This is how I have been doing it recently and means I can easily reuse the partial when ever a page uses this composition.

    Hopefully this is helpful

    Thanks

    Maxi

  • marcelh 171 posts 471 karma points
    Feb 26, 2016 @ 11:35
    marcelh
    0

    Ran into the issue today (again) when I want to use a single template for two documenttypes.

    Creating an interface that bind the two documenttypes didn't work unfortunately.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MasterContentModel<INewsContentModel>>
    

    This throws an InvalidOperationException

    The model item passed into the dictionary is of type 'ContentModels.MasterContentModel`1[ContentModels.NewsContentModel]', but this dictionary requires a model item of type 'ContentModels.MasterContentModel`1[ContentModels.INewsContentModel]'. 
    

    Did anyone find a workable solution for this?

Please Sign in or register to post replies

Write your reply to:

Draft