简体   繁体   中英

umbraco 7: get property value

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?

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.

以下代码对我有用:

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

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

item.NameofYourPropertyAlias

这是循环时正确使用 item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM