Copied to clipboard

Flag this post as spam?

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


  • Sebastian Dammark 583 posts 1407 karma points
    Dec 04, 2011 @ 23:08
    Sebastian Dammark
    0

    What is wrong with this if/else ?

    I'm trying my first Razor stuff and I'm producing questions like rabbits are producing offspring :)

    What's wrong with this:

    @if (node.title.Length != 0) {
        node.title;
    } else { 
        node.nodeName;
    }

    I get this error, which totally blows me off: error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    It sounds like something from Redmond :) 

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 05, 2011 @ 10:28
    Sebastiaan Janssen
    0

    The title has probably never been filled in, so it might be null instead of having no lengths, try this:

    @if (node.title != null && node.title.Length != 0) {
            node.title;
    } else { 
            node.nodeName;
    }

     

  • Sebastian Dammark 583 posts 1407 karma points
    Dec 05, 2011 @ 11:23
    Sebastian Dammark
    0

    Hmmm ... I've tried that but without any luck.
    I also changed node.nodeName to node.Name

    Still getting the same error 

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Dec 05, 2011 @ 11:46
    Chriztian Steinmeier
    0

    Hi Sebastia(a)n :-)

    I believe I read somewhere that you need to use aelement around standalone expressions like those (where you're not wrapping them in a or similar)

    So:

    @if (node.title.Length != 0) {
            <text>node.title</text>
    } else { 
            <text>node.nodeName</text>
    }

    /Chriztian

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 05, 2011 @ 11:53
    Sebastiaan Janssen
    0

    Ooh d'oh! of course, I didn't think of that. You could try that or just @node.Title and @node.Name (by the way, no need to type nodeName, just Name is fine).

    FYI: it depends if there's html before and after the @if. The context switching is a bit hard to figure out at first. :-)

  • Artem 4 posts 24 karma points
    Dec 08, 2011 @ 12:31
    Artem
    0

    If "node" is of type DynamicNode, e.g.:

    var node = Model.NodeById(1234);

    Than next script should work

    @if (!node.IsNull("title") && node.HasValue("title")) {
    @node.Title;
    } else {
    @node.Name;
    }
Please Sign in or register to post replies

Write your reply to:

Draft