简体   繁体   English

从MVC 2.0 Beta验证问题升级ASP.Net MVC RC 2.0

[英]ASP.Net MVC RC 2.0 Upgrade From MVC 2.0 Beta Validation Issue

We just recently upgraded our application and noticed that we're getting runtime errors on all of our Html.ValidationMessages and Html.ValidationMessageFor. 我们刚刚升级了我们的应用程序,发现我们在所有Html.ValidationMessages和Html.ValidationMessageFor上都遇到了运行时错误。 There error is: The given key was not present in the dictionary. 错误是: 字典中没有给定的密钥。 We've been passing the modelName/expression and it was working fine until the upgrade. 我们一直在传递modelName / expression,它在升级之前工作正常。 I also checked out the breaking changes as described in the readme but didn't notice anything that relates to it. 我还检查了自述文件中描述的重大更改,但没有注意到与之相关的任何内容。

After having debugged the ASP.NET MVC source I found the cause for this issue. 在调试ASP.NET MVC源代码后,我找到了导致此问题的原因。

You have to have <%= Html.EnableClientSideValidation %> before the <% Html.BeginForm %> , The reason for this is that when client side validation is enabled the Html form helper will generate an Id (formContext.FormId) that is used in the validation helper. 你必须在<%Html.BeginForm%>之前有<%= Html.EnableClientSideValidation%>,原因是当启用客户端验证时,Html表单助手将生成一个使用的Id(formContext.FormId)在验证助手中。 This means that if you are to use the validation helper you must use the built in form helpers. 这意味着如果要使用验证帮助程序,则必须使用内置表单帮助程序。

So to solve the issue just move the <%= Html.EnableClientSideValidation %> to above the first form and be sure to use the Html.BeginForm helper method. 因此,要解决此问题,只需将<%= Html.EnableClientSideValidation%>移动到第一个表单上方,并确保使用Html.BeginForm帮助程序方法。

I got the same error here, Levi, I knew you are on the asp.net mvc team, could you provide some guidance on that? 我在这里得到了同样的错误,Levi,我知道你在asp.net mvc团队,你能提供一些指导吗?

Here is error. 这是错误。

Line 185:                // rules will already have been written to the metadata object
Line 186:                fieldMetadata.ReplaceValidationMessageContents = (String.IsNullOrEmpty(validationMessage)); // only replace contents if no explicit message was specified
Line 187:                fieldMetadata.ValidationMessageId = builder.Attributes["id"];
Line 188:            }
Line 189:

Here is the trace. 这是痕迹。

[KeyNotFoundException: The given key was not present in the dictionary.]
   System.ThrowHelper.ThrowKeyNotFoundException() +29
   System.Collections.Generic.SortedDictionary`2.get_Item(TKey key) +5167507
   System.Web.Mvc.Html.ValidationExtensions.ValidationMessageHelper(HtmlHelper htmlHelper, ModelMetadata modelMetadata, String expression, String validationMessage, IDictionary`2 htmlAttributes) in D:\Projects\Practice\Mercurial\onlinerestaurant\trunk\ReferenceProjects\aspnetmvc2-rc-sources\src\SystemWebMvc\Mvc\Html\ValidationExtensions.cs:187
   System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression, String validationMessage, IDictionary`2 htmlAttributes) in D:\Projects\Practice\Mercurial\onlinerestaurant\trunk\ReferenceProjects\aspnetmvc2-rc-sources\src\SystemWebMvc\Mvc\Html\ValidationExtensions.cs:146
   System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression) in D:\Projects\Practice\Mercurial\onlinerestaurant\trunk\ReferenceProjects\aspnetmvc2-rc-sources\src\SystemWebMvc\Mvc\Html\ValidationExtensions.cs:131
   ASP.views_admin_createmenucategory_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\Projects\Practice\Mercurial\onlinerestaurant\trunk\OnlineRestaurant\Views\Admin\CreateMenuCategory.ascx:26
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) in D:\Projects\Practice\Mercurial\onlinerestaurant\trunk\ReferenceProjects\aspnetmvc2-rc-sources\src\SystemWebMvc\Mvc\ViewPage.cs:107
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM