Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • pawel 22 posts 42 karma points
    Dec 28, 2012 @ 23:02
    pawel
    0

    switch statement in Razor

    Hello Guys!

    I am trying to create google authorship for multiple authors of my site. I created basic syntax but it triggers error CS0151: A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type

     

    @switch(Model.redaktor){
    case "Adam":
        "<p>Adam Smith</p>";
        break;
    case "John":
        "<p>John Reed</p>";
        break;
    case "Ian":
        "<p>Ian Kowalski</p>";
        break;
    default:
        "<p>Unknown author</p>";
        break;
    }

    Any ideas where the problem is? Kind regards, Pawel

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Dec 28, 2012 @ 23:17
    Andy Butland
    0

    Your property will be a dynamic, so you should be OK if you cast it to a string.  I.e. 

    switch((string)Model.redaktor)
  • pawel 22 posts 42 karma points
    Dec 28, 2012 @ 23:23
    pawel
    0

    Thanks a lot Andy, but know I get: error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Dec 30, 2012 @ 14:08
    Andy Butland
    0

    I think now the problem is your quotes around the <p> tags.  You don't need them and I think razor is interpretted them as code.  Just to check have tested this in Razor and works OK...

        dynamic x = "abc";
        switch ((string)x) 
        {
            case "abc":
                <p>Hello</p>
                break;
        }
Please Sign in or register to post replies

Write your reply to:

Draft