【问题标题】:use page as argument in Umbraco在 Umbraco 中使用页面作为参数
【发布时间】:2017-09-06 06:16:09
【问题描述】:

我正在尝试在 Umbraco 中将 page 作为参数传递。在助手中,我需要页面的一些属性。比如名字,...

这是我的代码:

var PageWeAreInheritedFrom = CurrentPage;
    @ShowBanner(PageWeAreInheritedFrom);

@helper ShowBanner(dynamic pageWeRIn)
{
if (pageWeRIn.bannerIsInherited)
{
        @ShowBanner(pageWeRIn.Parent)
}
else
{
    //here I want to have a switch case based on pageWeRIn.Name 
    //but I cant have it. 
}
}

这是错误。似乎辅助方法中的页面类型不同

switch 表达式或 case 标签必须是 bool、char、string、 整数、枚举或对应的可空类型

【问题讨论】:

  • 该错误可能意味着由于 pageWeRIn 是动态的,因此开关不知道 pageWeRIn.Name 是哪种数据类型。如果您尝试将 pageWeRIn.Name 放入字符串变量中,然后根据该变量进行切换呢?
  • @JannikAnker 不知道为什么它不能被包裹在字符串中

标签: razor helper umbraco7 current-page


【解决方案1】:

这是因为pageWeRIn 是动态的,C# 的开关不能与动态变量一起使用。我个人在我的观点中不使用动态,而只使用类型化模型。更多信息请见:http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/

类型化的实现看起来像这样(未经测试):

@ShowBanner(Mode.Content);

@helper ShowBanner(IPublishedContent pageWeRIn)
{
    if (pageWeRIn.GetPropertyValue<bool>("bannerIsInherited"))
    {
        @ShowBanner(pageWeRIn.Parent)
    }
    else
    {
        //use all the switches you want on pageWeRIn.Name
    }
}

在不更改整个代码的情况下,另一种方法是引入一个输入的新变量(正如 Jannik 在他的评论中解释的那样),然后使用开关

string nodeName = pageWeRIn.Name
switch(nodeName){
    // whatever
}

【讨论】:

  • 感谢您的解决方案。你的意思是我最好使用 IPublishedContent 来保持 CurrentPage 变量?
  • CurrentPage 只是 Model.Content 的动态表示。动态更慢且 CPU 密集度更高,因为它们是在运行时解析的,因此在我看来,最好使用类型化对象(通过 UmbracoHelper)。特别是当您在视觉工作室(使用智能感知)中进行开发时。如果您在 Umbraco 后端编辑器(网络)中进行编辑,则 CurrentPage 可能会因为查询生成器而方便,但我个人从不使用它。
猜你喜欢
  • 1970-01-01
  • 2021-05-23
  • 2017-02-21
  • 2015-04-07
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多