2010-02-02 27 views
2

我的View中有一個Helper方法,它動態地呈現整個表單。現在我想爲Asp.Net MVC RC 2中的客戶端驗證添加支持。通過幫助程序方法動態調用Html.ValidationSummary()時發生KeyNotFound異常

以下是我用於將驗證摘要字段呈現到窗體上的代碼。

_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,字符串消息,IDictionary`2 htmlAttributes) at Fusion.UI.MvcWebUX.Helper.FormGenerator.GenerateFormValidation()in D:\ EBS \ Project.Fusion \ Fusion.UI.MvcWebUX \ Helper \ FormGenerator.cs:line 88 at Fusion.UI .MvcWebUX.Helper.FormGenerator.GenerateForm()in D:\ EBS \ Project.Fusion \ Fusion.UI.MvcWebUX \ Helper \ FormGenerator.cs:line 70 at Fusion.UI.MvcWebUX.Helper.FormGenerator.RenderForm(ViewPage viewPage )在D:\ EBS \ Project.Fusion \ Fusion.UI.MvcWebUX \ Helper \ FormGenerator.cs中:第60行 at ASP.views_shared_autoview_aspx .__ RenderContent2(HtmlTextWriter __w,Control parameterContainer)in d:\ EBS \ Project.Fusion \ Fusion.UI.MvcWebUX \ Views \ Shared \ AutoView.aspx:line 7 at System.Web.UI.Control。 RenderChildrenInternal(HtmlTextWriter writer,ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) at System.Web.UI.Control.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal( HtmlTextWriter的作家的ControlAdapter適配器) 在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家的ControlAdapter適配器) 在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家) 在ASP.views_shared_fusion_master .__ Render__control1(HtmlTextWriter的__w, Control parameterContainer)在d:\ EBS \ Project.F中usion \ Fusion.UI.MvcWebUX \ Views \ Shared \ Fusion.Master:line 74 at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer ) at System.Web.UI.Control.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer,ControlAdapter適配器) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) at System.Web.UI.Page.Render(HtmlT extWriter作家) 在System.Web.Mvc.ViewPage.Render(HtmlTextWriter作家) 在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter作家,ControlAdapter適配器) 在System.Web.UI.Control.RenderControl(HtmlTextWriter作家,的ControlAdapter適配器) 在System.Web.UI.Control.RenderControl(HtmlTextWriter的作家) 在System.Web.UI.Page.ProcessRequestMain(布爾includeStagesBeforeAsyncPoint,布爾includeStagesAfterAsyncPoint) 的InnerException:

我有這個掙扎幾個小時。任何人都可以幫助我解決它並告訴我爲什麼會發生這種情況。

+0

我已經發現問題了。 在編寫BeginForm之後,我一直在設置EnableClientValidation。 只需編寫_viewPage.Html.EnableClientValidation();之前在窗體上的任何事情和錯誤已解決。 謝謝, – Huzefa 2010-02-03 06:24:55

回答

0

我發現問題了。在編寫BeginForm之後,我一直在設置EnableClientValidation。只需編寫_viewPage.Html.EnableClientValidation();之前在窗體上的任何事情和錯誤已解決。謝謝,

+0

但是,這不是每個人的解決方案。 – Will 2010-03-26 17:36:13

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

或者如果您在母版頁中啓用了客戶端驗證,則可以使用'<%Html.EnableClientValidation(); %>'在之後' – ajbeaven 2011-05-31 23:23:44

相關問題