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

时间:2010-01-18 20:02:21

标签: asp.net-mvc

我们刚刚升级了我们的应用程序,发现我们在所有的Html.ValidationMessages和Html.ValidationMessageFor上都遇到了运行时错误。错误是:字典中没有给定的密钥。我们一直在传递modelName / expression,它在升级之前工作正常。我还检查了自述文件中描述的重大更改,但没有注意到与之相关的任何内容。

2 个答案:

答案 0 :(得分:3)

在调试ASP.NET MVC源代码后,我找到了导致此问题的原因。

您必须拥有<%= Html.EnableClientSideValidation%>在<%Html.BeginForm%>之前, 这样做的原因是,当启用客户端验证时,Html表单帮助程序将生成在验证帮助程序中使用的Id(formContext.FormId)。这意味着如果要使用验证帮助程序,则必须使用内置表单帮助程序。

所以要解决这个问题,只需移动<%= Html.EnableClientSideValidation%>到第一个表单之上,并确保使用Html.BeginForm帮助方法。

答案 1 :(得分:1)

我在这里遇到了同样的错误,Levi,我知道你在asp.net mvc团队,你能提供一些指导吗?

这是错误。

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:

这是追踪。

[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