Copied to clipboard

Flag this post as spam?

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


  • Natalie 22 posts 162 karma points
    Jul 04, 2019 @ 09:33
    Natalie
    0

    System.ArgumentNullException

    Hello !

    I try to handle a null exception with this code block

    @{
    IPublishedContent sectionPart = Umbraco.AssignedContentItem.AncestorOrSelf("sectionGeneric");
    IEnumerable<IPublishedContent> pageSectionSubtitle = sectionPart.Children();}
    <ul>
    @if (pageSectionSubtitle == null)
    {
        throw new System.ArgumentNullException();
    }
    @else (pageSectionSubtitle != null && pageSectionSubtitle.Count() > 0)
    {
        foreach (IPublishedContent page in pageSectionSubtitle)
        {
        <li>
            <h6>@page.Name</h6>
        </li>
        }
    }
    </ul>
    

    I don't know why but the else is not a valid term. I'm a student developper so, I think I forget Something. If you've got a clue, that'll be nice.

    Thanks

  • David Challener 80 posts 444 karma points c-trib
    Jul 08, 2019 @ 14:15
    David Challener
    0

    Hi Natalie,

    You're pretty close there in terms of syntax. I'm not quite sure what you're trying to achieve but to help get your snippet building you can do the following.

    In the razor syntax, you don't need an additional @ sign before the else. For if, then, else logic, you can't specify another condition after the else unless you say if again, so the final else is a way to say "in every other condition then do this"

    @if (condition)
    {
    }
    else if (another condition)
    {
    }
    else
    {
    }
    

    HTH, David

  • Erik-Jan Westendorp 29 posts 295 karma points MVP 4x c-trib
    Jul 09, 2019 @ 14:54
    Erik-Jan Westendorp
    0

    Hi Natalie,

    As David mentioned your syntax is not correct and you have to change it. Besides that your first if statement is not necessary and you can skip it. See the code below.

    @{
        var sectionPart = Umbraco.AssignedContentItem.AncestorOrSelf("sectionGeneric");
        var pageSectionSubtitle = sectionPart.Children();
    
    }
    <ul>
    
        @if(pageSectionSubtitle != null && pageSectionSubtitle.Any())
        {
            foreach (IPublishedContent page in pageSectionSubtitle)
            {
                <li>
                    <h6>@page.Name</h6>
                </li>
            }
        }
    </ul>
    
Please Sign in or register to post replies

Write your reply to:

Draft