Search code examples
c#asp.net-mvchtmlrazorumbraco7

Umbraco 7 - Get property from one page to another


Im new to umbraco 7

I have a Masterpage - Inside I have 2 Partialviews - HEADER & FOOTER

I have 2 subpages that inherites from Masterpage - HOME PAGE & STANDARD PAGE

Inside HOME PAGE - Document Type I have a Reletad Links property

The code for the Releted Links property is inside a Partialview called Links

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using Newtonsoft.Json.Linq

@{
    if (Model.Content.HasValue("externalLinks") && Model.Content.GetPropertyValue<string>("externalLinks").Length > 2)
    {
        <ul>
            @foreach (var item in Model.Content.GetPropertyValue<JArray>("externalLinks"))
            {
                var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;

                if (Model.Content.GetPropertyValue<JArray>("externalLinks").First() == item)
                {
                    <li>
                        <a href="@(item.Value<string>("link"))" target="@linkTarget"> <i class="fa fa-phone" aria-hidden="true"></i> @(item.Value<string>("caption"))</a>
                    </li>
                }
                else
                {
                    <li>
                        <a href="@(item.Value<string>("link"))" target="@linkTarget"> <i class="fa fa-flag" aria-hidden="true"></i> @(item.Value<string>("caption"))</a>
                    </li>
                }
            }
        </ul>
    }
}

The LINKS partialview is loaded inside the HEADER partialview

The code is working fine when navigating to HOME PAGE but If I go to STANDARD PAGE then the Reletad Links property is no loger visible.

What can I do to fix this.


Solution

  • Instead of referencing the current page (through Model.Content) you'll want to reference the homepage

    I've added a bit of code to the PartialView to illustrate it, haven't tested it though

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Newtonsoft.Json.Linq
    
    @{
        // First get the homepage by traversing the Umbraco tree and searching for the homepage node
        var homepage = Umbraco.TypedContentAtRoot().First().DescendantsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == "home");
        if (homepage != null && homepage.HasValue("externalLinks") && homepage.GetPropertyValue<string>("externalLinks").Length > 2)
        {
            <ul>
                @{
                    var externalLinks = homepage.GetPropertyValue<JArray>("externalLinks");
                    foreach (var item in externalLinks)
                    {
                        var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
    
                        if (externalLinks.First() == item)
                        {
                            <li>
                                <a href="@(item.Value<string>("link"))" target="@linkTarget"> <i class="fa fa-phone" aria-hidden="true"></i> @(item.Value<string>("caption"))</a>
                            </li>
                        }
                        else
                        {
                            <li>
                                <a href="@(item.Value<string>("link"))" target="@linkTarget"> <i class="fa fa-flag" aria-hidden="true"></i> @(item.Value<string>("caption"))</a>
                            </li>
                        }
                    }
                }
            </ul>
        }
    }