Copied to clipboard

Flag this post as spam?

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


  • Natalie 21 posts 161 karma points
    17 days ago
    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 65 posts 281 karma points c-trib
    1 week ago
    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 2 posts 71 karma points
    1 week ago
    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