0

I'm trying to pass page as argument in Umbraco . and in a helper I need some properties of the page . like Name , ...

This is my code :

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. 
}
}

This is the Error .seems the page type is different in the helper method

A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type

Sara N
  • 1,079
  • 5
  • 17
  • 45
  • The error probably means that because pageWeRIn is dynamic the switch has no idea which datatype pageWeRIn.Name is. What if you try putting pageWeRIn.Name into a string variable and then do the switch based on that? – Jannik Anker Apr 11 '17 at 08:08
  • @JannikAnker don't have any idea why it couldn't be wrapped in string – Sara N Apr 11 '17 at 23:12

1 Answers1

0

This is caused because pageWeRIn is dynamic and C#'s switch can't work with dynamic variables. I personally don't work with dynamics in my views but only with typed models. For more information see: http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/

A typed implementation would look somehthing like this (not tested):

@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
    }
}

Another way to do it without changing the whole code would be to introduce a new variable that's typed (as Jannik explained in his comment) and then use a switch

string nodeName = pageWeRIn.Name
switch(nodeName){
    // whatever
}
Mark
  • 3,231
  • 3
  • 32
  • 57
  • thanks for the solution . you mean I've better to use IPublishedContent for keeping CurrentPage variable? – Sara N Apr 11 '17 at 23:21
  • CurrentPage is only a dynamic representation of Model.Content. Dynamics are slower and more cpu intensive because they are resolved at runtime, thus in my opinion it's better to use typed objects (via UmbracoHelper). Especially when you're developing in visual studio (with intellisense). If you're editing in the Umbraco backend editor (web) CurrentPage can be handy because of the query builder, but I personally never use that. – Mark Apr 12 '17 at 07:31