I have a simple controller that receives as a parameter an IList<MyObject> where MyObject has 2 integer fields, defined as:
public class MyObject
{
public int First { get; set; }
public int Second { get; set; }
}
In the view, I have 4 fields, to enter information about 2 MyObjects object. When I submit the form with non numeric data, the validation triggers as expected, and the controller redirects the user to the same view, which renders the fields with the red
border. However, if I press the submit button once again in there, I get a NullReferenceException. What am I doing wrong? The exception is triggered even before the controller starts.
Here's the stack trace:
[NullReferenceException: Object reference not set to an instance of an object.] System.Web.Mvc.DefaultModelBinder.UpdateCollection(ModelBindingContext bindingContext, Type itemType) +613 System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext
bindingContext) +519 System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext bindingContext) +829 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ParameterInfo parameterInfo) +313 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(MethodInfo
methodInfo) +399 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +232 System.Web.Mvc.Controller.ExecuteCore() +152 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +86 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext
requestContext) +28 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +332 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +55 System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +28
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +358 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64
It's possible that there's a discrepancy between how the binder is trying to read the wire data and how it's being rendered in the form. Can we see your view code and action method signature? Thanks! :)
Hi. I just found my problem. It seems that when you bind, and use a hidden field to set the index for the IList, you shouldn't use Html.Hidden helper method, but write a hand coded input hidden field instead.
If you use the helper, it will start concatenating the different indexes one after the other, causing the problem described above.
Yeah. The code's a bit fragile, unfortunately. :( We're still hashing out modifications such that you don't have to use the weird Index hidden fields or that we'll provide nicer error messages if something goes wrong. Nothing is finalized, though.
pablin
0 Points
2 Posts
NullReferenceException after validation problems
Nov 06, 2008 04:56 PM|LINK
Hi,
I have a simple controller that receives as a parameter an IList<MyObject> where MyObject has 2 integer fields, defined as:
public class MyObject { public int First { get; set; } public int Second { get; set; } }In the view, I have 4 fields, to enter information about 2 MyObjects object. When I submit the form with non numeric data, the validation triggers as expected, and the controller redirects the user to the same view, which renders the fields with the red border. However, if I press the submit button once again in there, I get a NullReferenceException. What am I doing wrong? The exception is triggered even before the controller starts.
Here's the stack trace:
[NullReferenceException: Object reference not set to an instance of an object.] System.Web.Mvc.DefaultModelBinder.UpdateCollection(ModelBindingContext bindingContext, Type itemType) +613 System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext bindingContext) +519 System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext bindingContext) +829 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ParameterInfo parameterInfo) +313 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(MethodInfo methodInfo) +399 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +232 System.Web.Mvc.Controller.ExecuteCore() +152 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +86 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +28 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +332 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +55 System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +28 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +358 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64
Any idea? Thank you in advance.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: NullReferenceException after validation problems
Nov 06, 2008 06:11 PM|LINK
It's possible that there's a discrepancy between how the binder is trying to read the wire data and how it's being rendered in the form. Can we see your view code and action method signature? Thanks! :)
pablin
0 Points
2 Posts
Re: NullReferenceException after validation problems
Nov 06, 2008 06:18 PM|LINK
Hi. I just found my problem. It seems that when you bind, and use a hidden field to set the index for the IList, you shouldn't use Html.Hidden helper method, but write a hand coded input hidden field instead.
If you use the helper, it will start concatenating the different indexes one after the other, causing the problem described above.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: NullReferenceException after validation problems
Nov 06, 2008 06:23 PM|LINK
Yeah. The code's a bit fragile, unfortunately. :( We're still hashing out modifications such that you don't have to use the weird Index hidden fields or that we'll provide nicer error messages if something goes wrong. Nothing is finalized, though.