diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs index 0a15584962..06b9968f4a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs @@ -455,16 +455,10 @@ namespace Microsoft.AspNet.Mvc.Rendering string tag) { var formContext = ViewContext.ClientValidationEnabled ? ViewContext.FormContext : null; - - if (ViewData.ModelState.IsValid == true) + if (ViewData.ModelState.IsValid && (formContext == null || excludePropertyErrors)) { - if (formContext == null || - ViewContext.UnobtrusiveJavaScriptEnabled && - excludePropertyErrors) - { - // No client side validation/updates - return HtmlString.Empty; - } + // No client side validation/updates + return HtmlString.Empty; } string wrappedMessage; @@ -483,6 +477,8 @@ namespace Microsoft.AspNet.Mvc.Rendering wrappedMessage = null; } + // If excludePropertyErrors is true, describe any validation issue with the current model in a single item. + // Otherwise, list individual property errors. var htmlSummary = new StringBuilder(); var modelStates = ValidationHelpers.GetModelStateList(ViewData, excludePropertyErrors); @@ -514,7 +510,7 @@ namespace Microsoft.AspNet.Mvc.Rendering var divBuilder = new TagBuilder("div"); divBuilder.MergeAttributes(htmlAttributes); - if (ViewData.ModelState.IsValid == true) + if (ViewData.ModelState.IsValid) { divBuilder.AddCssClass(HtmlHelper.ValidationSummaryValidCssClassName); } @@ -525,23 +521,10 @@ namespace Microsoft.AspNet.Mvc.Rendering divBuilder.InnerHtml = wrappedMessage + unorderedList.ToString(TagRenderMode.Normal); - if (formContext != null) + if (formContext != null && !excludePropertyErrors) { - if (ViewContext.UnobtrusiveJavaScriptEnabled) - { - if (!excludePropertyErrors) - { - // Only put errors in the validation summary if they're supposed to be included there - divBuilder.MergeAttribute("data-valmsg-summary", "true"); - } - } - else - { - // client validation summaries need an ID - divBuilder.GenerateId("validationSummary", IdAttributeDotReplacement); - formContext.ValidationSummaryId = divBuilder.Attributes["id"]; - formContext.ReplaceValidationSummary = !excludePropertyErrors; - } + // Inform the client where to replace the list of property errors after validation. + divBuilder.MergeAttribute("data-valmsg-summary", "true"); } return divBuilder.ToHtmlString(TagRenderMode.Normal); @@ -631,13 +614,12 @@ namespace Microsoft.AspNet.Mvc.Rendering return GetValidationAttributes(name, metadata: null); } - // Only render attributes if unobtrusive client-side validation is enabled, and then only if we've - // never rendered validation for a field with this name in this form. Also, if there's no form context, - // then we can't render the attributes (we'd have no
"); - - // TODO revive viewContext.OutputClientValidation(), this requires GetJsonValidationMetadata(), GitHub #163 _viewContext.FormContext = null; } diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewContext.cs b/src/Microsoft.AspNet.Mvc.Core/ViewContext.cs index 7f3c47ca9d..25024e8049 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewContext.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewContext.cs @@ -27,7 +27,6 @@ namespace Microsoft.AspNet.Mvc Writer = writer; _formContext = _defaultFormContext; - UnobtrusiveJavaScriptEnabled = true; ClientValidationEnabled = true; ValidationSummaryMessageElement = "span"; ValidationMessageElement = "span"; @@ -41,7 +40,6 @@ namespace Microsoft.AspNet.Mvc : base(viewContext) { _formContext = viewContext.FormContext; - UnobtrusiveJavaScriptEnabled = viewContext.UnobtrusiveJavaScriptEnabled; ClientValidationEnabled = viewContext.ClientValidationEnabled; ValidationSummaryMessageElement = viewContext.ValidationSummaryMessageElement; ValidationMessageElement = viewContext.ValidationMessageElement; @@ -64,8 +62,6 @@ namespace Microsoft.AspNet.Mvc } } - public bool UnobtrusiveJavaScriptEnabled { get; set; } - public bool ClientValidationEnabled { get; set; } ///