Page view counter

NullReferenceException after validation problems

Last post 11-06-2008 2:23 PM by levib. 3 replies.

Sort Posts:

  • NullReferenceException after validation problems

    11-06-2008, 12:56 PM
    • Loading...
    • pablin
    • Joined on 11-06-2008, 5:51 PM
    • Posts 2
    • Points 0

    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.

  • Re: NullReferenceException after validation problems

    11-06-2008, 2:11 PM
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 683
    • Points 3,661

    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! :)

  • Re: NullReferenceException after validation problems

    11-06-2008, 2:18 PM
    • Loading...
    • pablin
    • Joined on 11-06-2008, 5:51 PM
    • Posts 2
    • Points 0

     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.

  • Re: NullReferenceException after validation problems

    11-06-2008, 2:23 PM
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 683
    • Points 3,661

    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.

Page 1 of 1 (4 items)