tags:

views:

217

answers:

3

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.

A: 

Were you able to resolve this? I am experiencing exactly this now, and have not had any luck resolving this.

Jim
No unfortunately not, I'm going to report it
RailRhoad
Can you provide a stack trace?
Levi
I have the same issue here, when I enable <% Html.EnableClientValidation(); %>The error is in D:\Projects\Practice\Mercurial\onlinerestaurant\trunk\ReferenceProjects\aspnetmvc2-rc-sources\src\SystemWebMvc\Mvc\Html\ValidationExtensions.cs Line: 187
J.W.
-1 for not being an answer
Carl Hörberg
+1  A: 

Hello, 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
J.W.
Thanks for posting this, I got wrapped up in something else
RailRhoad
+2  A: 

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.

Torkel
Awesome, checking it out in the next few hours
RailRhoad