ASP.NET MVC验证问题

时间:2021-03-02 16:36:42

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 objects of type TestModel, properly indexed.When I submit the form with invalid data, I get the view with the errors highlighted.However, when I re-submit it (without changing anything), I get this error:

一个非常简单的视图,有两个TestModel类型的对象,正确索引。当我提交带有无效数据的表单时,我得到了突出显示错误的视图。但是,当我重新提交它(没有改变任何东西)时,我得到了这个错误:

[NullReferenceException: Object reference not set to an instance of an object.] System.Web.Mvc.DefaultModelBinder.UpdateCollection(ModelBindingContext bindingContext, Type itemType) +612 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

[NullReferenceException:对象引用未设置为对象的实例。] System.Web.Mvc.DefaultModelBinder.UpdateCollection(ModelBindingContext bindingContext,Type itemType)+612 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()+ 155 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 htt pContext)+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 on how can I debug this?

关于如何调试这个的任何想法?

3 个解决方案

#1


I was already looking at that article, and found the bug I was having (subtle, yet critical).If you render the hidden field with the index using Html.Hidden, the helper will "accumulate" the previous values, so you'll end up with a hidden saying index=1, and the next saying index=1,2.

我已经看过那篇文章了,发现了我遇到的错误(微妙但又很关键)。如果你使用Html.Hidden渲染带索引的隐藏字段,帮助器将“累积”以前的值,所以你会最后有一个隐藏的说法索引= 1,下一个说法索引= 1,2。

Changing the helper call to a manually coded hidden field fixed the issue.

将帮助程序调用更改为手动编码的隐藏字段可解决此问题。

#2


Not sure I can answer without seeing more of the code and how your form is setup.But, you could take a look at Phil Haack's blog entry about Model Binding To A List.
Hope this helps.

我不确定如果没有看到更多的代码以及你的表单是如何设置的,我就可以回答。但是,你可以看看Phil Haack关于Model Binding To A List的博客文章。希望这可以帮助。

#3


Thanks that fixed it!

谢谢你修好了!

I replaced

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

with

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

Should we report this as a bug - It would be nice to have it fixed for ASP.Net MVC RC1

我们应该将此报告为一个错误 - 为ASP.Net MVC RC1修复它会很好

#1


I was already looking at that article, and found the bug I was having (subtle, yet critical).If you render the hidden field with the index using Html.Hidden, the helper will "accumulate" the previous values, so you'll end up with a hidden saying index=1, and the next saying index=1,2.

我已经看过那篇文章了,发现了我遇到的错误(微妙但又很关键)。如果你使用Html.Hidden渲染带索引的隐藏字段,帮助器将“累积”以前的值,所以你会最后有一个隐藏的说法索引= 1,下一个说法索引= 1,2。

Changing the helper call to a manually coded hidden field fixed the issue.

将帮助程序调用更改为手动编码的隐藏字段可解决此问题。

#2


Not sure I can answer without seeing more of the code and how your form is setup.But, you could take a look at Phil Haack's blog entry about Model Binding To A List.
Hope this helps.

我不确定如果没有看到更多的代码以及你的表单是如何设置的,我就可以回答。但是,你可以看看Phil Haack关于Model Binding To A List的博客文章。希望这可以帮助。

#3


Thanks that fixed it!

谢谢你修好了!

I replaced

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

with

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

Should we report this as a bug - It would be nice to have it fixed for ASP.Net MVC RC1

我们应该将此报告为一个错误 - 为ASP.Net MVC RC1修复它会很好