umbraco 7: get property value

13,345

Solution 1

Big derp on my side.

The item var in the foreach loop already has property's contained in the item.

all you need to do is

item.NameofYourPropertyAlias

and that should do it.

Solution 2

The following code works for me:

item[index].GetProperty("name").Value

Solution 3

Makes it worse is intellicense Isn't working as its all runtime.

The problem is that you're using dynamic model instead of the strongly typed kind.

Instead of using CurrentPage you can use Model.Content.

So instead of:

var homePage = CurrentPage.AncestorsOrSelf(1).First();

Use:

var homePage = Model.Content.AncestorOfSelf(1);

Intellisense will then work after you type a stop after homePage.

I would also strongly recommend inputting using Umbraco.web with the rest of your using statements at the top of the template. This should further improve intellisense, and allow you to see methods such as .GetPropertyValue<T>(string alias).

You should then be able to use item.GetPropertyValue<string>("navigationColor").

Share:
13,345
lemunk
Author by

lemunk

BY DAY: Senior Software Dev @ Iforce Ltd (RouteGenie department) BY NIGHT: New Father, Gamer, Catch up on tech, Sleep when i can. FOR FUN: Dable in unity, been diving into xamarin, and New JS libs.

Updated on June 23, 2022

Comments

  • lemunk
    lemunk almost 2 years

    I am using umbraco 7 ASP.NET C# MVC 4.

    Im trying to use the new umbraco, so far its been ok, but i need to get a property of one of my pages i set up.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
         var homePage = CurrentPage.AncestorsOrSelf(1).First();
    
         var menuItems = homePage.Children.Where("isMenu == true");
    
    }
    <!-- Nav -->
    
    <div class="row">
    <div class="col-md-12 top-menu-container">
        <ul>
            @* If the Url of the current page is "/" then we want to add the class "current_page_item" *@
            @* Otherwise, we set the class to null, that way it will not even be added to the <li> element *@
            <li>
                <div onclick="location.href='/';"  class="col-md-2 metro-container" style="background-color:#@Model.Content.GetPropertyValue("navigationColor")" >
                    <img class="menu-img" src="../../media/1001/home.png" alt="" />
                    Home
                </div>
            </li>
    
            @foreach (var item in menuItems)
            {
                @* If the Id of the item is the same as the Id of the current page then we want to add the class "current_page_item" *@
                @* Otherwise, we set the class to null, that way it will not even be added to the <li> element *@
                
                <li>
                    <div onclick="location.href='@item.Url';"  class="col-md-2 metro-container" style="background-color:#@item.Content.GetPropertyValue("navigationColor")" >
                        <img class="menu-img" src="../../media/1001/home.png" alt="" />
                        @item.Name
                    </div>
                </li>
            }
        </ul>
    </div>
    </div>
    

    So the first "li" outside the loop i simply get the models content Getproperty method which surely enough gets me any property i tell it too.

    However the loop i have although goes through the current pages children, i cant seem to get a specific property.

    Makes it worse is intellicense Isn't working as its all runtime.

    The line in question is

    <div onclick="location.href='@item.Url';"  class="col-md-2 metro-container" style="background-color:#@item.Content.GetPropertyValue("navigationColor")" >
    

    I need to get the color that was set on the page from the navigationColor control.

    What am i doing wrong here?