许多复选框时出现InvalidOperationException?

时间:2012-09-06 08:55:00

标签: c# asp.net asp.net-mvc iis

我有一个表单,允许用户更改两种对象之间的依赖关系。

我有一个有30个元素的集合,对于这30个元素中的每一个,我可以选择另一个集合的26个元素中的一个,几个或全部。所以它基本上意味着我有30 * 26个复选框(由于业务逻辑,复杂但复杂)。我可能会在同一页面上多次这样做,所以我想我可能会有1100多个元素。

它基本上存储在这个模型中:

public class VarDependency{
   public int Id { get; set; }
   public String Name { get; set; }
   public List<ValueDependency> Values { get; set; }
}

public class ValueDependency{
   public int Id { get; set; }
   public String Name{ get; set; }
   public List<AvailableValue> Triggers { get; set; }
}

public class AvailableValue{
   public bool IsSelected { get; set; }
   public String Text { get; set; }
   public int Value { get; set; }
}

在视图中,我基本上有两个循环。对于每个可用的值,我有一个LabelFor(基于Text属性),一个复选框(基于IsSelected)和一个隐藏字段(基于Value)。

当我没有这么多元素(像15 * 15这样的东西)时,一切都运转良好。我在控制器中收到数据,我可以处理它们,但我不知道为什么,如果我得到更多的元素,我会很快得到异常,然后才能进入控制器:

System.InvalidOperationException 由于对象的当前状态,操作无效。

System.Web.HttpException (0x80004005): The URL-encoded form data is not valid. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded()
   at System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding)
   at System.Web.HttpRequest.FillInFormCollection()
   at System.Web.HttpRequest.FillInFormCollection()
   at System.Web.HttpRequest.get_Form()
   at Microsoft.Web.Infrastructure.DynamicValidationHelper.ValidationUtility.CollectionReplacer.<>c__DisplayClass8.<MakeCollectionsLazy>b__2()
   at Microsoft.Web.Infrastructure.DynamicValidationHelper.ValidationUtility.CollectionReplacer.<>c__DisplayClass12.<ReplaceCollection>b__e()
   at Microsoft.Web.Infrastructure.DynamicValidationHelper.ValidationUtility.CollectionReplacer.<>c__DisplayClass12.<ReplaceCollection>b__11()
   at Microsoft.Web.Infrastructure.DynamicValidationHelper.DeferredCountArrayList.get_Count()
   at System.Collections.Specialized.NameObjectCollectionBase.get_Count()
   at System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, RequestValidationSource requestCollection)
   at System.Web.HttpRequest.get_Form()
   at System.Web.HttpRequestWrapper.get_Form()
   at System.Web.Mvc.HttpRequestExtensions.GetHttpMethodOverride(HttpRequestBase request)
   at System.Web.Mvc.AcceptVerbsAttribute.IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
   at System.Web.Mvc.HttpPostAttribute.IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
   at System.Web.Mvc.ActionMethodSelector.<>c__DisplayClass11.<RunSelectionFilters>b__d(ActionMethodSelectorAttribute attr)
   at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Web.Mvc.ActionMethodSelector.RunSelectionFilters(ControllerContext controllerContext, List`1 methodInfos)
   at System.Web.Mvc.ActionMethodSelector.FindActionMethod(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.ReflectedControllerDescriptor.FindAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.ControllerActionInvoker.FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, String actionName)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
   at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
   at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

即使在Visual Studio的调试模式下,我也不会被抛出异常。

我找不到有关如何解决此问题的任何信息,因此我们非常感谢任何帮助,当我们对该版本进行一些测试时会出现问题:/

1 个答案:

答案 0 :(得分:3)

我认为这可能是由于最近推出的页面可以拥有的默认最大控件数。您可以在web.config中更改此设置 看看这个问题:

ASP.NET MS11-100: how can I change the limit on the maximum number of posted form values?