Copied to clipboard

Flag this post as spam?

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


  • Paul 24 posts 114 karma points
    Aug 21, 2015 @ 12:37
    Paul
    0

    Using Custom Controllers & Models, how to get CurrentPage from UmbracoViewPage?

    Hi, I am attempting to use a custom model and controller. I have got so far successfully, but I cant fathom how to get 'Current Page', or its equivilent. I have been following the docs here but must be missing something.

    This is a simplified snippet of my old view code that works with the default controller:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    if (CurrentPage.HasValue("subtitle"))
    {
        <span>@CurrentPage.subtitle</span>
    }
    

    This is how it now looks:

    @using Project.Models;
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<myModel>
    if (CurrentPage.HasValue("subtitle"))
    {
        <span>@CurrentPage.subtitle</span>
    }
    

    and this is my model (partially based on this blog post):

    using Umbraco.Web;
    using Umbraco.Web.Models;
    
    namespace Project.Models
    {
        public class myModel : RenderModel
        {
            public myModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
    
            public string variable { get; set; }
        }
    }
    

    I cannot resolve CurrentPage in my view. I have tried Model.View.CurrentPage and Model.Content.View.CurrentPage but am having no luck. How do I get CurrentPage?

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Aug 21, 2015 @ 13:47
    Alex Skrypnyk
    1

    Hi Paul,

    Try to use like this :

    if (Model.GetPropertyValue("subtitle") != null)
    {
        <span>@Model.GetPropertyValue("subtitle")</span>
    }
    

    Model is your page now.

    Thanks,

    Alex

    We do top quality development.

    http://www.ukad-group.com/

  • Paul 24 posts 114 karma points
    Aug 21, 2015 @ 13:53
    Paul
    0

    Thanks Alex, I have used Model.Content.GetProperty(...). I think I must have found that as you were typing your answer!

  • 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