KeyNotFound通过helper方法动态调用Html.ValidationSummary()时发生异常

时间:2010-02-02 08:59:58

标签: c# javascript asp.net-mvc keynotfoundexception

我的视图中有一个帮助方法,它动态呈现整个表单。现在我想在Asp.Net MVC RC 2中添加对客户端验证的支持。

以下是我用于将验证摘要字段呈现到表单上的代码。

_viewPage.Html.EnableClientValidation();
        MvcHtmlString validationSummary = _viewPage.Html.ValidationSummary("There are errors on this form. Please contact your administrator.");
        if (validationSummary != null && validationSummary.ToString() != "")
            Response.Write(validationSummary.ToString());

当代码到达这一点时,我得到以下错误。

System.Collections.Generic.KeyNotFoundException was unhandled by user code

Message =“字典中没有给定的密钥。”   来源=“系统”   堆栈跟踪:        在System.ThrowHelper.ThrowKeyNotFoundException()        在System.Collections.Generic.SortedDictionary 2.get_Item(TKey key) at System.Web.Mvc.Html.ValidationExtensions.ValidationSummary(HtmlHelper htmlHelper, Boolean excludePropertyErrors, String message, IDictionary 2 htmlAttributes)        在System.Web.Mvc.Html.ValidationExtensions.ValidationSummary(HtmlHelper htmlHelper,String message,IDictionary`2 htmlAttributes)        在D:\ EBS \ Project.Fusion \ Fusion.UI.MvcWebUX \ Helper \ FormGenerator.cs中的Fusion.UI.MvcWebUX.Helper.FormGenerator.GenerateFormValidation():第88行        在D:\ EBS \ Project.Fusion \ Fusion.UI.MvcWebUX \ Helper \ FormGenerator.cs中的Fusion.UI.MvcWebUX.Helper.FormGenerator.GenerateForm():第70行        在D:\ EBS \ Project.Fusion \ Fusion.UI.MvcWebUX \ Helper \ FormGenerator.cs中的Fusion.UI.MvcWebUX.Helper.FormGenerator.RenderForm(ViewPage viewPage):第60行        at ASP.views_shared_autoview_aspx .__ RenderContent2(HtmlTextWriter __w,Control parameterContainer)在d:\ EBS \ Project.Fusion \ Fusion.UI.MvcWebUX \ Views \ Shared \ AutoView.aspx:第7行        在System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)        在System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)        在System.Web.UI.Control.Render(HtmlTextWriter编写器)        在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter)        在System.Web.UI.Control.RenderControl(HtmlTextWriter编写器,ControlAdapter适配器)        在System.Web.UI.Control.RenderControl(HtmlTextWriter writer)        at ASP.views_shared_fusion_master .__ Render__control1(HtmlTextWriter __w,Control parameterContainer)在d:\ EBS \ Project.Fusion \ Fusion.UI.MvcWebUX \ Views \ Shared \ Fusion.Master:第74行        在System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)        在System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)        在System.Web.UI.Control.Render(HtmlTextWriter编写器)        在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter)        在System.Web.UI.Control.RenderControl(HtmlTextWriter编写器,ControlAdapter适配器)        在System.Web.UI.Control.RenderControl(HtmlTextWriter writer)        在System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)        在System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)        在System.Web.UI.Page.Render(HtmlTextWriter编写器)        在System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)        在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter)        在System.Web.UI.Control.RenderControl(HtmlTextWriter编写器,ControlAdapter适配器)        在System.Web.UI.Control.RenderControl(HtmlTextWriter writer)        在System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)   InnerException:

我已经挣扎了几个小时。任何人都可以帮我解决它并告诉我为什么会发生这种情况。

2 个答案:

答案 0 :(得分:1)

我已经两次来这里寻求解决方案,所以我也会回答另一件可能导致这种情况的事情。

在MVC2 RC中(我相信;随着2010 RC提供的版本),您可以在调用EnableClientValidation时导致此异常:

<%-- This is the proper way to call these two methods --%>
<% Html.ValidationSummary(); %>
<% Html.EnableClientValidation(); %>

这种方式会导致异常:

<%-- Side effects of calling ECV first will result in the exception --%>
<% Html.EnableClientValidation(); %>
<% Html.ValidationSummary(); %>

从像这样的几个断开连接的方法调用中出现副作用非常糟糕。有点让我想起全局变量。

答案 1 :(得分:0)

我发现了它的问题。在编写BeginForm之后,我一直在设置EnableClientValidation。只需编写_viewPage.Html.EnableClientValidation();在表单上的任何内容之前,错误已解决。谢谢,