KeyNotFound Exception when calling Html.ValidationSummary() dynamically through helper method
I have a Helper Method in my View which renders the whole form dynamically. Now i want to add a support for Client Validation as available in Asp.Net MVC RC 2.
Following is the code i use to render Validation Summary field onto the Form.
_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());
When code reaches this point i get following error.
System.Collections.Generic.KeyNotFoundException was unhandled by user code
Message="The given key was not present in the dictionary."
Source="System"
StackTrace:
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.SortedDictionary2.get_Item(TKey key)
at System.Web.Mvc.Html.ValidationExtensions.ValidationSummary(HtmlHelper htmlHelper, Boolean excludePropertyErrors, String message, IDictionary
2 htmlAttributes)
at System.Web.Mvc.Html.ValidationExtensions.ValidationSummary(HtmlHelper htmlHelper, String message, IDictionary`2 htmlAttributes)
at Fusion.UI.MvcWebUX.Helper.FormGenerator.开发者_JAVA百科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) in D:\EBS\Project.Fusion\Fusion.UI.MvcWebUX\Helper\FormGenerator.cs:line 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 writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at ASP.views_shared_fusion_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\EBS\Project.Fusion\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 adapter)
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(HtmlTextWriter writer)
at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
I have struggling with this for few hours. Can anyone help me to solve it and tell me why would this be happening.
I've come here twice for the solution, so I'll also answer with another thing that can cause this.
In MVC2 RC (I believe; the version delivered with 2010 RC), you can cause this exception when calling EnableClientValidation
:
<%-- This is the proper way to call these two methods --%>
<% Html.ValidationSummary(); %>
<% Html.EnableClientValidation(); %>
This way will cause the exception:
<%-- Side effects of calling ECV first will result in the exception --%>
<% Html.EnableClientValidation(); %>
<% Html.ValidationSummary(); %>
Pretty crappy to have side effects crop up from a couple disconnected method calls like this. Kinda reminds me of global variables.
I have found the problem to it. I had been setting EnableClientValidation after writing BeginForm. Just write _viewPage.Html.EnableClientValidation(); before anything on the Form and the error was solved. Thanks,
精彩评论