Copied to clipboard

Flag this post as spam?

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


  • Peter 24 posts 102 karma points
    Mar 15, 2017 @ 06:42
    Peter
    0

    How to: Properties available in View and Partials

    Hi

    I’m working at a site there we have our own member handling. We need to check if a member is logged in (this check was previously in the master view – I have now moved it to a custom default controller) and redirect the member if he is accessing certain pages. The problem is – that we in the view and partials need some information about the member (saved in session state). The way it is handled now is – that we get the session information (and store it in a local variable) in the Masterview - and in partials if the information is needed. I believe it is not the correct way to do it.

    Is it not possible – now I have made a custom default controller – to populate a model with the needed information, which will be accessible from the Masterview and all subsequent partials?

  • Peter 24 posts 102 karma points
    Mar 15, 2017 @ 10:38
    Peter
    0

    I have tried doing the following:

    Created a Model called CustomRenderModel which is a subclass of RenderModel

    public class CustomRenderModel : RenderModel
    {
    
        public CustomRenderModel(IPublishedContent content) : base(content) { }
        public CustomClassX ClassX { get; set; }
        public CustomClassY ClassY{ get; set; }
        public Dictionary<string, string> Messages = new Dictionary<string, string>();
    }
    

    And done the following in the Custom Default Controller:

    public class CustomDefaultController : Umbraco.Web.Mvc.RenderMvcController
    {
               public override ActionResult Index(RenderModel model)
               {
                   CustomRenderModel customModel = new CustomRenderModel(model.Content);
    
                   customModel.ClassX = DummySystem.GetClassX;
                   customModel.ClassY = DummySystem.GetClassy;
    
                   return base.Index(customModel);
               }
    }
    

    I’m still inheriting from Umbraco.Web.Mvc.UmbracoTemplatePage in the Master View – but when I need to access the extra information in the CustomRenderModel from the view – I first have to cast it .e.g. ((CustomRenderModel) Model).ClassX

    Can that really be true? – Do I need to cast the Model - or is there a better way?

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 15, 2017 @ 11:23
    Alex Skrypnyk
    0

    Hi Peter

    Try to use this code:

    @inherits UmbracoTemplatePage<CustomRenderModel>
    

    You will be able to work with Model properties and Umbraco helper.

    Also try to avoid UmbracoTemplatePage, and do not use dynamic CurrentPage, the better way is to use UmbracoViewPage:

    @inherits UmbracoViewPage<CustomRenderModel>
    

    Thanks,

    Alex

  • Peter 24 posts 102 karma points
    Mar 15, 2017 @ 11:48
    Peter
    0

    Thanks for you answer Alex

    I have just tried

    @inherits UmbracoTemplatePage<CustomRenderModel>
    

    But I get this error:

    The type ’web.Models.CustomRenderModel’ cannot be used as type parameter ‘TContent’ in the generic type or method ‘Umbraco.TemplatePage

    Any idea what is wrong? - Am I missing something in my CustomRenderModel or CustomDefaultController?

    The people who have made the website have used CurrentPage - which is the reason I'm trying with the UmbracoTemplatePage.

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 15, 2017 @ 12:03
    Alex Skrypnyk
    0

    Peter, maybe you need to change return statement in controller to :

    return CurrentTemplate(customModel);
    
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 15, 2017 @ 13:02
    Alex Skrypnyk
    0

    Peter, this topic motivated me to rewrite similar issue on one of our projects, and I would say that it works. I used

    @inherits UmbracoViewPage<CustomRenderModel> 
    

    and

    return CurrentTemplate(customModel);
    
  • Peter 24 posts 102 karma points
    Mar 15, 2017 @ 15:29
    Peter
    1

    Hi

    Thanks for your effort - I really appreciate it.

    I have only tried with @inherits UmbracoTemplatePage<CustomRenderModel> and not @inherits UmbracoViewPage<CustomRenderModel> since CurrentPage is used in the template.

    Perhaps it is not possible to use @inherits UmbracoTemplatePage<CustomRenderModel> (or I am just not able to figure it out). We use version 7.3 - perhaps it is because we do not use the latest version.

  • Peter 24 posts 102 karma points
    Mar 16, 2017 @ 06:33
    Peter
    100

    I think @inherits UmbracoTemplatePage<CustomRenderModel> first is possible from version 7.4 - which explains why I'm not able to use it :-)

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 16, 2017 @ 09:19
    Alex Skrypnyk
    0

    Hi Peter

    Great that you found out what is the issue. I didn't think that Umbraco version can be the reason :)

    /Alex

  • 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