Copied to clipboard

Flag this post as spam?

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


  • Simon Goldsmith 14 posts 33 karma points
    Nov 08, 2013 @ 00:11
    Simon Goldsmith
    0

    Get parent pageName or field from parent page/node?

    I am not very experienced with Umbraco so please bare with me, this is about my 2/3 build;

    I have a page called About us with sub pages called History, Our Story, where to find us, etc.

    On the About Us page the title of the page comes from Field("title") or .pageName, on the subsequest pages I want to also use the "title" from the About Us page as well as the title from the current sub page, like so:

    Parentpage.Field("title") - CurrentPage.Field("title")

    How do I do this using razor?

    Thanks
    Simon

  • Fuji Kusaka 2203 posts 4220 karma points
    Nov 08, 2013 @ 02:29
    Fuji Kusaka
    0

    Hi Simon,

    If you want to have the Current page name using the field title you can just do something like this

    if(Model.HasValue("title")){
    Model.title 
    }
    else{
    Model.Name
     } 

    or 

    var titlePage = Model.HasValue("title")? Model.title : Model.Name; 
    @titlePage

    for the parent name

    Model.Parent.Name or 
    var parentName = Model.Parent.HasValue("title")? Model.Parent.title : Model.Parent.Name;
    @parentName 
  • Simon Goldsmith 14 posts 33 karma points
    Nov 08, 2013 @ 11:18
    Simon Goldsmith
    0

    If I try and use the code you have mentioned in my umbraco template I get the following error:

    <h1 class="page-title">
                        @{
                            var parentTitle = Model.Parent.HasValue("title")? Model.Parent.title : Model.Parent.Name;
                        }
                        @parentTitle
                    </h1>
    

    CS0119: 'Umbraco.Core.Models.ContentExtensions.Parent(Umbraco.Core.Models.IMedia)' is a 'method', which is not valid in the given context

    I have tried using the @Model bit before and I got the same issues; Am I missing a key bit of information or library?

  • Fuji Kusaka 2203 posts 4220 karma points
    Nov 08, 2013 @ 11:24
    Fuji Kusaka
    0

    Hang on are you using this code in a template or in a cshtml file ?

    If you are using it in a template you need to wrap the code like this

    <umbraco:Macro runat="server" language="cshtml">
    @using umbraco.MacroEngines; @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    var parentTitle = Model.Parent.HasValue("title")? Model.Parent.title : Model.Parent.Name;
    @parentTitle
    }
    </umbraco:Macro> 
  • Simon Goldsmith 14 posts 33 karma points
    Nov 08, 2013 @ 15:47
    Simon Goldsmith
    0

    OK, So I need to use a partial view macro in Umbraco to display code like this? Is that correct?

  • Fuji Kusaka 2203 posts 4220 karma points
    Nov 08, 2013 @ 15:48
    Fuji Kusaka
    0

    are you using v6 ? am not very familiar with v6 

  • Simon Goldsmith 14 posts 33 karma points
    Nov 08, 2013 @ 16:04
    Simon Goldsmith
    0

    I am using Umbraco 6.1.6 and asp.Net MVC + Razor.

    I just need a way to show a field form the parent-page on the child page, whether this be in my template or in a partial view. :) Thanks!

  • Simon Goldsmith 14 posts 33 karma points
    Nov 08, 2013 @ 18:36
    Simon Goldsmith
    0

    Ive put this code in a partial view but it errors as below

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @if(!Model.Parent != null){
        <h1>@Model.Parent.Name</h1>
    }
    

    Operator '!' cannot be applied to operand of type 'method group'

  • Tim 66 posts 89 karma points
    Dec 03, 2017 @ 09:53
    Tim
    1

    If anyone's looking to do this, I found Model.Parent.Name wouldn't work, but Model.Content.Parent.Name would

Please Sign in or register to post replies

Write your reply to:

Draft