[英]umbraco 7: get property value
我正在使用 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 方法,这肯定足以让我得到任何我告诉它的属性。
然而,我的循环虽然通过了当前页面的孩子,但我似乎无法获得特定的属性。
更糟糕的是 intellicense 不能像它的所有运行时一样工作。
有问题的线路是
<div onclick="location.href='@item.Url';" class="col-md-2 metro-container" style="background-color:#@item.Content.GetPropertyValue("navigationColor")" >
我需要从 navigationColor 控件获取页面上设置的颜色。
我在这里做错了什么?
我身边的大derp。
foreach 循环中的项目 var 已经在项目中包含了属性。
你需要做的就是
item.NameofYourPropertyAlias
那应该这样做。
以下代码对我有用:
item[index].GetProperty("name").Value
更糟糕的是 intellicense 不能像它的所有运行时一样工作。
问题是您使用的是动态模型而不是强类型模型。
您可以使用Model.Content
而不是使用CurrentPage
。
所以而不是:
var homePage = CurrentPage.AncestorsOrSelf(1).First();
用:
var homePage = Model.Content.AncestorOfSelf(1);
在homePage
后键入 stop 后,Intellisense 将起作用。
我还强烈建议using Umbraco.web
输入模板顶部的其余 using 语句。 这应该会进一步改进智能感知,并允许您查看诸如.GetPropertyValue<T>(string alias)
。
然后您应该能够使用item.GetPropertyValue<string>("navigationColor")
。
item.NameofYourPropertyAlias
这是循环时正确使用 item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.