简体   繁体   中英

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. 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. 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.

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. 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.

I got the same error here, Levi, I knew you are on the asp.net mvc team, could you provide some guidance on that?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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