3

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?

4

4 回答 4

2

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.

于 2014-05-16T13:38:55.933 回答
1

The following code works for me:

item[index].GetProperty("name").Value
于 2015-04-07T07:54:01.047 回答
1

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").

于 2016-01-14T14:15:09.343 回答
-2
item.NameofYourPropertyAlias

this is the proper use of item while looping

于 2014-05-16T13:50:34.553 回答