ASP.NET MVC problem with validation

I have this simple controller:

public class OneController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Create()
    {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(IList<TestModel> m)
    {
        return View(m);
    }
}

      

And a very simple view with two TestModel objects indexed correctly. When I submit a form with invalid data, I get a submission with highlighted errors. However, when I re-submit it (without changing anything) I get this error:

[NullReferenceException: Object reference not set to instance object.] System.Web.Mvc.DefaultModelBinder.UpdateCollection (ModelBindingContext bindingContext, ItemType type) +612 System.Web.Mvc.DefaultModelBindModelCore (ModelBindingContext) + System5 bindingContext. Web.Mvc.DefaultModelBinder.BindModel (ModelBindingContext bindingContext) +829 System.Web.Mvc.ControllerActionInvoker.GetParameterValue (ParameterInfo parameterInfo) +313 System.Web.Mvc.ControllerActionInvfooker.GetParameter SystemValues ​​(Method +3Web.GetInfo method SystemValues) .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 (SystemContepContext) +28 .Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest (HttpContext httpContext) +28 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +358 System.Webtep.Httpecution completedSynchronously) +64ProcessRequest (HttpContext httpContext) +28 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +358 System.Web.HttpApplication.ExecuteStep (IExecutionStep + step completed, Boolean &ously)ProcessRequest (HttpContext httpContext) +28 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +358 System.Web.HttpApplication.ExecuteStep (IExecutionStep + step, Boolean & Exercise)

Any idea on how I can debug this?

0


source to share


3 answers


I have already reviewed this article and found the error I ran into (subtle but critical). If you render a hidden field using an index using Html.Hidden, the helper will "accumulate" the previous values, so you get a hidden index of index = 1 and the next index = 1,2.



Changing the helper call to a hand-coded hidden field fixes the issue.

+3


source


Not sure if I can answer without seeing more code and how your form is set up. But you can take a look at Phil Haack's blog post Model Snap to List .
Hope it helps.



+1


source


Thanks for fixing it!

I replaced

<%= Html.Hidden("submitFormFields.index", controlID) %>

      

from

<input type="hidden" id="submitFormFields.index" name="submitFormFields.index" value="<%=controlID %>" />

      

Should we report this as a bug? It would be nice if this was fixed for ASP.Net MVC RC1

+1


source







All Articles