简体   繁体   English

Umbraco 7:将属性映射到页面并检查是否与当前页面关联

[英]Umbraco 7: Map property to page and check if is associated with current page

In my Content section I have a property editor (Archetype) that allows to set content for the site independently from the content tree. 在“内容”部分中,我有一个属性编辑器(Archetype),该编辑器允许独立于内容树来设置网站的内容。 I need to display only the sub categories from one category based on what page I'm currently on. 我只需要根据我当前所在的页面显示一个类别中的子类别。 What I have now is: 我现在所拥有的是:

//get the content with id of 1123 from Content Section, type DynamicPublishedContent
var catItems = Umbraco.Content(1123).categoryItem; 

foreach (var item in catItems)
{
  foreach (var sub in item.GetValue<ArchetypeModel>("subCatItem"))
  {
    <div class="tbl_dt">
        <p class="offerName">@sub.GetValue("offerName")</p>
        <p class="departurePort">@sub.GetValue("departurePort")</p>
    </div>
 }
}

See this reference for other details: Umbraco 7: Get fields from same property based on current page 有关其他详细信息,请参见此参考: Umbraco 7:基于当前页面从同一属性获取字段

Q: How can I map the property to a content page and check if is associated with current page and display only the fields with mapped current page? 问:如何将属性映射到内容页面,并检查是否与当前页面关联并仅显示映射了当前页面的字段? Can this be done by adding a content picker to it? 可以通过向其添加content picker来完成此操作吗? If so how could I check if it is associated with current page? 如果是这样,我如何检查它是否与当前页面相关联?

First it is not good practice to reference content in code by their id. 首先,按ID引用代码中的内容并不是一种好习惯。 Rather use the document type alias which is guaranteed not to get deleted by a user. 而是使用保证不会被用户删除的文档类型别名。

Now to check for the existence of a property on the current page all you need to do is 现在,要检查当前页面上是否存在属性,您需要做的就是

@if (CurrentPage.HasValue("subCatItem"))
{
   string propertyStoredValue = CurrentPage.subCatItem.ToString();
}

Where "subCatItem" is the alias of the property you are checking for. 其中“ subCatItem”是您要检查的属性的别名。 The type of the property is not relevant in this case and bear in mind if the property is not mandatory and has not been given a value the above statement will evaluate to false even though the property exists on the document type (makes sense?) 在这种情况下,属性的类型无关紧要,请记住,如果该属性不是强制性的,并且未赋予其值,则即使该属性存在于文档类型上,上述语句也将评估为false(有意义吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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