2014-05-16 82 views
2

用一把umbraco嗨7 ASP.NET C#MVC 4一把umbraco 7:獲取屬性值

我試着去使用新的一把umbraco,至今其一直不錯,但我需要得到之一的屬性我我設置的頁面。

@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> 

所以第一個「禮」外循環,我只是得到了模型的內容getProperty方法這肯定足以讓我的財產我告訴它了。

但是,我的循環雖然經歷了當前網頁的兒童,我似乎無法得到一個特定的屬性。

使情況變得更糟是不是所有運行時都在工作。

有問題的行是

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

我需要獲得從navigationColor控制在頁面上設置的顏色。

我在這裏做錯了什麼?

回答

2

在我身邊的大銳角。

foreach循環中的項目var已經包含在項目中的屬性。

所有你需要做的是

item.NameofYourPropertyAlias 

而且應該這樣做。

-2
item.NameofYourPropertyAlias 

這是正確使用商品,而循環

1

下面的代碼對我的作品:

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

使情況變得更糟是intellicense不工作作爲其所有運行時。

問題是您正在使用動態模型而不是強類型。

而不是使用CurrentPage您可以使用Model.Content

所以不是:

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

用途:然後

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

智能感知將您homePage後鍵入停止後工作。

我還強烈建議在模板的頂部輸入using Umbraco.web以及其餘的使用語句。這應該會進一步改進智能感知,並允許您查看諸如.GetPropertyValue<T>(string alias)等方法。您可以使用item.GetPropertyValue<string>("navigationColor")