U4-6721 - Error when submitting Macros, Collection was modified; enumeration operation may not execute. (after project has been updated to MVC5)

Created by Timothy 21 May 2015, 06:50:15 Updated by andrew shearer 12 Oct 2015, 03:24:32

All my Umbraco forms appear and render correctly initially (using Umbraco 7.2.5), however when they are submitted the following error appears:

Error loading Partial View script (file: ~/Views/MacroPartials/InsertUmbracoForm.cshtml)

The record is saved to the form entries if it was valid, and oddly if you refresh after the submission error it shows the success message, refreshing again shows the empty form as expected

Here's the log:

2015-05-21 16:36:18,906 [40] WARN umbraco.macro - [Thread 61] Error loading Partial View (file: ~/Views/MacroPartials/InsertUmbracoForm.cshtml). Exception: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext() at System.Web.Mvc.ModelStateDictionary.Merge(ModelStateDictionary dictionary) at Umbraco.Web.Mvc.ControllerExtensions.EnsureViewObjectDataOnResult(ControllerBase controller, ViewResultBase result) at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode node) at umbraco.macro.LoadPartialViewMacro(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)

1 Attachments

Comments

Timothy 21 May 2015, 23:37:51

This problem seems to happen when a solution is upgraded to MVC 5


Gavin Faux 11 Jun 2015, 11:43:41

Reproduced in 7.2.6 / Umbraco Forms 4.1.3 with site upgraded to MVC5


Tim Geyssens 17 Jun 2015, 14:25:26

Seems like an Umbraco core issue, also happens with a simple surface controller


Tim Geyssens 17 Jun 2015, 14:32:21

Full error Server Error in '/' Application.

Collection was modified; enumeration operation may not execute.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

Source Error:

Line 59: Line 60: Line 61: @Umbraco.RenderMacro("TestMacro") Line 62: Line 63: @Html.Action("Index","Test")

Source File: c:\Users\timgeyssens\Documents\Visual Studio 2013\Projects\SupportFormsMVC5\SupportFormsMVC5\Views\Master.cshtml Line: 61

Stack Trace:

[InvalidOperationException: Collection was modified; enumeration operation may not execute.] System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52 System.Collections.Generic.Enumerator.MoveNext() +10900893 System.Web.Mvc.ModelStateDictionary.Merge(ModelStateDictionary dictionary) +167 Umbraco.Web.Mvc.ControllerExtensions.EnsureViewObjectDataOnResult(ControllerBase controller, ViewResultBase result) +206 Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult) +107 Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content) +1140 Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode node) +239 umbraco.macro.LoadPartialViewMacro(MacroModel macro) +153 umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId) +2261 umbraco.macro.renderMacro(Hashtable attributes, Hashtable pageElements, Int32 pageId) +57 Umbraco.Web.UmbracoHelper.RenderMacro(macro m, IDictionary2 parameters, page umbracoPage) +943 Umbraco.Web.UmbracoHelper.RenderMacro(String alias, IDictionary2 parameters, page umbracoPage) +266 Umbraco.Web.UmbracoHelper.RenderMacro(String alias, IDictionary2 parameters) +198 Umbraco.Web.UmbracoHelper.RenderMacro(String alias, Object parameters) +111 Umbraco.Web.UmbracoHelper.RenderMacro(String alias) +69 ASP._Page_Views_Master_cshtml.Execute() in c:\Users\timgeyssens\Documents\Visual Studio 2013\Projects\SupportFormsMVC5\SupportFormsMVC5\Views\Master.cshtml:61 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +122 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +145 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer) +41 System.Web.WebPages.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer) +335 System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +42 System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content) +45 System.Web.WebPages.WebPageBase.Write(HelperResult result) +53 System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action1 body) +178 System.Web.WebPages.WebPageBase.PopContext() +229 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +154 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +380 Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) +151 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +429 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +106 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +1841 System.Web.Mvc.Controller.ExecuteCore() +118 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +229 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39 Umbraco.Web.Mvc.UmbracoPageResult.ExecuteControllerAction(ControllerContext context, IController controller) +110 Umbraco.Web.Mvc.UmbracoPageResult.ExecuteResult(ControllerContext context) +434 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +106 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +62 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9658396 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


Tim Geyssens 17 Jun 2015, 14:37:20

Example project: https://www.dropbox.com/s/0hkuc293r5lskn6/SupportMVC5.zip?dl=0


Jason Elkin 25 Jun 2015, 10:48:12

We had the same issue.

On a hunch I upgraded Microsoft.AspNet.WebApi (and it's dependencies) to the latest version (5.2.3).

This has solved the issue for us and not created any others as of yet.


Sebastiaan Janssen 08 Jul 2015, 18:30:00

I've tried your example project Tim but it works flawlessly for me. Even added @Html.Action("Index","Test") in the Master.cshtml as shown in the error message but I can submit both forms with no problem.


Sebastiaan Janssen 08 Jul 2015, 18:31:35

I also tried this in a separate solution doing my own upgrade of MVC after a similar support request and had no problems there either. I do notice that you didn't update the Views\web.config file with the newer versions. But I don't think that could explain why this solution works on my machine and not yours.


Jason Elkin 09 Jul 2015, 08:41:01

@sebastiaan what version of Microsoft.AspNet.WebApi are you using? I found that if I upgrade Microsoft.AspNet.WebApi (and it's dependencies) to the latest version (5.2.3) that I am able to submit forms, but validation still doesn't work.


Tim Geyssens 09 Jul 2015, 09:08:16

So what could be different in our environments, I'm using vs 2013


Sebastiaan Janssen 09 Jul 2015, 14:36:42

@timgeyssens Dunno.. are you still getting the error with this solution? Also VS13 here. Try restoring all NuGet packages maybe I got newer ones when I restored?


Sebastiaan Janssen 21 Jul 2015, 12:23:14

Okay, it's reproducible if you do something like the following in your controller: ModelState.AddModelError("Avatar", "Please upload an avatar that is at least 400x400 pixels");


Sebastiaan Janssen 21 Jul 2015, 12:24:37

Note: if I leave the key empty it works fine: ModelState.AddModelError("", "Please upload an avatar that is at least 400x400 pixels");


Sebastiaan Janssen 21 Jul 2015, 13:55:49

Commit made by '''Sebastiaan Janssen''' on ''2015-07-21T15:55:39+02:00'' https://github.com/umbraco/Umbraco-CMS/commit/34f44b02a2f3da95ac76ee93a1129dc103a5f6d6

U4-6721 Error when submitting Macros, Collection was modified; enumeration operation may not execute. (after project has been updated to MVC5)

#U4-6721 In Progress


Sebastiaan Janssen 21 Jul 2015, 13:58:07

That was actually not true. So the problem was with MVC5's Merge method that was changing values in the dictionary while iterating over that same dictionary. Something seems to have changed for MVC5 where this is not allowed, or implemented differently.

Anyway, it seems all fixed with this commit, I've made sure that everywhere we call Merge it goes through our new SafeMerge now and I've also updated our Merge with prefix method so that should work for MVC5 too.

@Shandem if you could please review?


Sebastiaan Janssen 21 Jul 2015, 17:35:15

Commit made by '''Sebastiaan Janssen''' on ''2015-07-21T19:35:00+02:00'' https://github.com/umbraco/Umbraco-CMS/commit/0bbe1c6cdcecd201fe2d0f7eba0aa8d9b5d724f2

U4-6721 Error when submitting Macros, Collection was modified; enumeration operation may not execute. (after project has been updated to MVC5)

#U4-6721 In Progress


Sebastiaan Janssen 21 Jul 2015, 17:39:49

Right, so that did not work at all. The problem, as it turns out, seems to be that we're doing things at the wrong time and to the wrong dictionaries, reverted everything and fixed ControllerExtensions:

  • Can only update result.ViewData by copying it into a temp variable first, updating the temp var's values and then setting result.ViewData to that temp var
  • Only AFTER that can we update result.ViewData.ModelState

Now this works and the modelstate doesn't get totally wiped out like in my previous commit (d'oh!).

So please review latest commit only, this is now tested properly and works though I'm not entirely 100% sure why we can only update the ModelState values only after updating the ViewData (it's late and I'm tired).


Shannon Deminick 22 Jul 2015, 11:55:03

Reviewing now


Sebastiaan Janssen 28 Jul 2015, 05:56:34

Commit made by '''Sebastiaan Janssen''' on ''2015-07-21T17:05:31+02:00'' https://github.com/umbraco/Umbraco-CMS/commit/aa9b188836eb15b0e92d83f12d94973f10d90bb3

U4-6721 Error when submitting Macros, Collection was modified; enumeration operation may not execute. (after project has been updated to MVC5)

#U4-6721 In Progress

(cherry picked from commit 34f44b02a2f3da95ac76ee93a1129dc103a5f6d6)


Sebastiaan Janssen 28 Jul 2015, 05:56:34

Commit made by '''Sebastiaan Janssen''' on ''2015-07-21T19:40:26+02:00'' https://github.com/umbraco/Umbraco-CMS/commit/1e60c6298d54eb6482013d09cf75d164c5ce41d3

U4-6721 Error when submitting Macros, Collection was modified; enumeration operation may not execute. (after project has been updated to MVC5)

#U4-6721 In Progress

(cherry picked from commit 0bbe1c6cdcecd201fe2d0f7eba0aa8d9b5d724f2)


Pankaj 25 Aug 2015, 11:41:07

How can I obtain this fix.


Shannon Deminick 25 Aug 2015, 12:15:29

The fix will be part of 7.3, you can upgrade to it when it is out or you can use the RC that is already available.


Pankaj 25 Aug 2015, 12:42:00

Thanks @Shandem for you quick reply. As mentioned in the RC that this is because of MVC 5 update. Is it possible to downgrade to MVC 4, are there any risk in doing that.


Shannon Deminick 25 Aug 2015, 12:56:56

7.3 is built on MVC 5 and you cannot downgrade 7.3 to use MVC 4.

This entire issue in this thread is due to the behavior of MVC 5. If you don't want to use MVC 5 in any version before 7.3, that is fine. That is what we ship with.


Dennis Öhman 01 Sep 2015, 11:16:27

The 7.2.8-fixedmodelstate package fails to install with error

Update-Package UmbracoCms -Version 7.2.8-fixedmodelstate -Source https://www.myget.org/F/umbraconightly/api/v2

Update-Package : Unable to resolve dependency 'UmbracoCms.Core (= 7.2.8-fixedmodelstate)'. At line:1 char:1

  • Update-Package UmbracoCms -Version 7.2.8-fixedmodelstate -Source https://www.myg ...
  •   + CategoryInfo          : NotSpecified: (:) [Update-Package], Exception
      + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.UpdatePackageCommand
    


Sebastiaan Janssen 01 Sep 2015, 11:23:43

@madsoulswe Make sure to add -Pre to that command as it's treated as a pre-release.


Dennis Öhman 01 Sep 2015, 13:36:08

@sebastiaan thanks, it worked!

Except that it destroyed views/web.config, some kind of merge happend without asking. And I can't use Newtonsoft.Json 7.0.1, only 6.0.5. But it works!


andrew shearer 12 Oct 2015, 03:24:32

hi - what was the fix for this?


Priority: Normal

Type: Bug

State: Fixed

Assignee: Shannon Deminick

Difficulty:

Category:

Backwards Compatible: True

Fix Submitted:

Affected versions:

Due in version: 7.3.0

Sprint:

Story Points:

Cycle: