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?
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?
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.
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?
I have tried doing the following:
Created a Model called
CustomRenderModel
which is a subclass ofRenderModel
And done the following in the Custom Default Controller:
I’m still inheriting from
Umbraco.Web.Mvc.UmbracoTemplatePage
in the Master View – but when I need to access the extra information in theCustomRenderModel
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?
Hi Peter
Try to use this code:
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:
Thanks,
Alex
Thanks for you answer Alex
I have just tried
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
orCustomDefaultController
?The people who have made the website have used CurrentPage - which is the reason I'm trying with the UmbracoTemplatePage.
Peter, maybe you need to change return statement in controller to :
Peter, this topic motivated me to rewrite similar issue on one of our projects, and I would say that it works. I used
and
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.I think
@inherits UmbracoTemplatePage<CustomRenderModel>
first is possible from version 7.4 - which explains why I'm not able to use it :-)Hi Peter
Great that you found out what is the issue. I didn't think that Umbraco version can be the reason :)
/Alex
is working on a reply...