Razor 在Umbraco中将页面用作参数

Razor 在Umbraco中将页面用作参数,razor,helper,umbraco7,current-page,Razor,Helper,Umbraco7,Current Page,我正试图在Umbraco中通过page作为参数。在助手中,我需要页面的一些属性。像名字 这是我的代码: var PageWeAreInheritedFrom = CurrentPage; @ShowBanner(PageWeAreInheritedFrom); @helper ShowBanner(dynamic pageWeRIn) { if (pageWeRIn.bannerIsInherited) { @ShowBanner(pageWeRIn.Parent) }

我正试图在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. 
}
}
这是错误。帮助器方法中的页面类型似乎不同

开关表达式或大小写标签必须是bool、char、string、, 整数、枚举或相应的可为空类型


这是因为
pageWeRIn
是动态的,而C#的开关不能处理动态变量。我个人在视图中不使用动力学,只使用类型化模型。有关更多信息,请参阅:

类型化实现的外观如下(未经测试):


该错误可能意味着,因为pageWeRIn是动态的,所以交换机不知道pageWeRIn.Name是哪种数据类型。如果您尝试将pageWeRIn.Name放入一个字符串变量,然后根据该变量进行切换,会怎么样?@JannikAnker不知道为什么不能将其包装在string中感谢您的解决方案。你是说我最好使用IPPublishedContent来保持CurrentPage变量?CurrentPage只是Model.Content的动态表示。由于动态是在运行时解析的,所以它的速度较慢,cpu占用更大,因此我认为最好使用类型化对象(通过UmbracoHelper)。尤其是在visual studio(使用intellisense)中开发时。如果您正在使用Umbraco后端编辑器(web)进行编辑,则由于查询生成器,CurrentPage非常方便,但我个人从未使用过它。
@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
    }
}
string nodeName = pageWeRIn.Name
switch(nodeName){
    // whatever
}