【问题标题】:umbraco 7: get property valueumbraco 7:获取属性值
【发布时间】:2014-05-16 13:19:53
【问题描述】:

我正在使用 umbraco 7 ASP.NET C# MVC 4。

我正在尝试使用新的 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>

所以循环外的第一个“li”我只需获取模型内容 Getproperty 方法,这肯定足以让我得到我告诉它的任何属性。

尽管我的循环通过了当前页面子项,但我似乎无法获得特定属性。

更糟糕的是智能感知不能作为它的所有运行时工作。

有问题的行是

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

我需要从 navigationColor 控件中获取页面上设置的颜色。

我在这里做错了什么?

【问题讨论】:

    标签: c# asp.net-mvc-4 umbraco7


    【解决方案1】:

    我这边的大笨蛋。

    foreach 循环中的 item var 已经具有包含在 item 中的属性。

    你需要做的就是

    item.NameofYourPropertyAlias
    

    应该这样做。

    【讨论】:

      【解决方案2】:

      以下代码适用于我:

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

      【讨论】:

        【解决方案3】:

        更糟糕的是智能感知不能作为它的所有运行时工作。

        问题在于您使用的是动态模型而不是强类型类型。

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

        所以而不是:

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

        用途:

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

        在您在homePage 之后键入停止后,Intellisense 就会起作用。

        我还强烈建议在模板顶部输入 using Umbraco.web 以及其余的 using 语句。这应该会进一步改进智能感知,并允许您查看诸如.GetPropertyValue&lt;T&gt;(string alias) 之类的方法。

        然后您应该可以使用item.GetPropertyValue&lt;string&gt;("navigationColor")

        【讨论】:

          【解决方案4】:
          item.NameofYourPropertyAlias
          

          这是在循环中正确使用item

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多