Copied to clipboard

Flag this post as spam?

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


  • Jason 17 posts 37 karma points
    Dec 22, 2015 @ 17:39
    Jason
    0

    Weird error: Collection was modified; enumeration operation may not execute.

    Hey guys,

    So we've had a macro for a sidebar that's been working for a while. It's pretty straight forward, may even be what Umbraco ships anyway (I wasn't around for the initial install and didn't check to see what Umbraco has by default in ways of a sidebar). It just figures out the root of the page you're on and builds a tree down from there. Nothing fancy.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ 
        @*Get the root of the website *@
        var root = CurrentPage.AncestorOrSelf(2);
    }
    
    <ul class="nav nav-pills nav-stacked subnav">
        @foreach (var page in root.Children.Where("Visible"))
        { 
            <li class="@page.IsAncestorOrSelf(CurrentPage, "active", "")">
                <a href="@page.Url">@page.Name</a>
                @if (page.Children != null && page.Children.Count() > 0) {
                    <ul class="subnav">
                    @foreach (dynamic subpage in page.Children.Where("Visible")){
                        <li class="@subpage.IsAncestorOrSelf(CurrentPage, "active", "")">
                             <a href="@subpage.Url">@subpage.Name</a>
                        </li>
                    }               
                    </ul>
                }
            </li>
        }
    </ul>
    

    It gets called like this:

    @Umbraco.RenderMacro("Subnav")
    

    We've recently done some work with custom controllers (we had been for a while, but only recently started having the issue).

    On initial page, the sub navigation loads just fine. However, when the page posts to itself (the same or different controller, doesn't matter, only that the URL stays the same) the sidebar throws an error. We're using BeginUmbracoForm to contain all of our form posts.

    Before you say "it's recent, so look through the recent code and figure out what's wrong", we've done that. And I still can't figure out what's wrong. ;)

    The error we get is "Error loading partial view. Exception: System.InvalidOperationException: Collection was modified; enumeration operation may not execute."

    The most bizarre thing about it is even when I empty the file completely, I still get the error. The rest of the page loads fine, only the RenderMacro method when rendering this partial generates the error.

    Have any of you come across this? Any ideas what COULD be causing the problem? We're not modifying the collection that gets looped over in the sidebar at all, and even if we were that wouldn't explain the blank file also throwing the error. It's almost as if the problem is in RenderMacro itself, rather than in the partial we're rendering via the macro.

    This has stumped me for several days. Any insight is appreciated.

    Thanks!

    -jason

  • Jason 17 posts 37 karma points
    Dec 22, 2015 @ 19:53
    Jason
    0

    I actually figured this out, finally. It has to do with MVC5 and the issue that was fixed with Umbraco 7.3. We can't upgrade yet, and we inadvertently upgraded MVC. A rollback to MVC4 fixed the issue.

    -j

Please Sign in or register to post replies

Write your reply to:

Draft