获取 属性 值而不仅仅是 Id
Get property value instead of just Id
我在 Umbraco 解决方案中有这一行,它为我提供了所选节点的 ID,以逗号分隔。
但我想在 oneliner 中得到的是所选节点的值。
所以目前它只是 return 节点 ID,例如“1001、1000、1003”,如果只选择一个,则只是“1003”。
我想要它 return 的是所选节点的值。假设它是我想要的 属性 上的菜单标题。所以我想要 "Frontpage, Contact, About Us" 等
而不是上面的输出
那看起来怎么样?
var mynode = Page.Cache.GetById(id);
mynode.GetPropertyString("pages")
最简单的方法是安装 Umbraco Core Property Value Converters nuget 包。然后,这将为您提供一种扩展方法,您可以使用该方法一次性检索节点列表。然后,您可以使用 LINQ 来 select 每个页面的 menuTitle
属性。
var menuTitles = mynode.GetPropertyValue<IEnumerable<IPublishedContent>>("pages")
.Select(x => x.GetPropertyValue<string>("menuTitle"));
如果需要,您可以使用 string.Join()
组合菜单标题以获得 comma-separated 列表。
以下是您可能需要添加的 using 语句列表:
using System.Collections.Generic;
using System.Linq;
using Our.Umbraco.PropertyConverters;
using Umbraco.Web;
using Umbraco.Core.Models;
我在 Umbraco 解决方案中有这一行,它为我提供了所选节点的 ID,以逗号分隔。
但我想在 oneliner 中得到的是所选节点的值。 所以目前它只是 return 节点 ID,例如“1001、1000、1003”,如果只选择一个,则只是“1003”。
我想要它 return 的是所选节点的值。假设它是我想要的 属性 上的菜单标题。所以我想要 "Frontpage, Contact, About Us" 等
而不是上面的输出那看起来怎么样?
var mynode = Page.Cache.GetById(id);
mynode.GetPropertyString("pages")
最简单的方法是安装 Umbraco Core Property Value Converters nuget 包。然后,这将为您提供一种扩展方法,您可以使用该方法一次性检索节点列表。然后,您可以使用 LINQ 来 select 每个页面的 menuTitle
属性。
var menuTitles = mynode.GetPropertyValue<IEnumerable<IPublishedContent>>("pages")
.Select(x => x.GetPropertyValue<string>("menuTitle"));
如果需要,您可以使用 string.Join()
组合菜单标题以获得 comma-separated 列表。
以下是您可能需要添加的 using 语句列表:
using System.Collections.Generic;
using System.Linq;
using Our.Umbraco.PropertyConverters;
using Umbraco.Web;
using Umbraco.Core.Models;