Configure ValidationSummary in ASP.NET MVC 2

I want to configure html output for ValidationSummary in ASP.NET MVC 2

of

<div class="validation-summary-errors"> <span>Oops! validation was failed because:</span> <ul> <li>The Title field is required.</li> <li>The Body field is required.</li> </ul> </div> 

to

 <div class="validation-error"> <p>Oops! validation was failed because:</p> <ul> <li>The Title field is required.</li> <li>The Body field is required.</li> </ul> </div> 

Is there any new way in asp.net MVC 2 to solve this problem?

+4
source share
2 answers

There seems to be no way to do this with templates, which is pretty bad. If you look at the code for this particular helper method, you will see that the HTML is baked in the method itself:

 public static string ValidationSummary(this HtmlHelper htmlHelper, string message, IDictionary<string, object> htmlAttributes) { // Nothing to do if there aren't any errors if (htmlHelper.ViewData.ModelState.IsValid) { return null; } string messageSpan; if (!String.IsNullOrEmpty(message)) { TagBuilder spanTag = new TagBuilder("span"); spanTag.MergeAttributes(htmlAttributes); spanTag.MergeAttribute("class", HtmlHelper.ValidationSummaryCssClassName); spanTag.SetInnerText(message); messageSpan = spanTag.ToString(TagRenderMode.Normal) + Environment.NewLine; } else { messageSpan = null; } StringBuilder htmlSummary = new StringBuilder(); TagBuilder unorderedList = new TagBuilder("ul"); unorderedList.MergeAttributes(htmlAttributes); unorderedList.MergeAttribute("class", HtmlHelper.ValidationSummaryCssClassName); foreach (ModelState modelState in htmlHelper.ViewData.ModelState.Values) { foreach (ModelError modelError in modelState.Errors) { string errorText = GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, null /* modelState */); if (!String.IsNullOrEmpty(errorText)) { TagBuilder listItem = new TagBuilder("li"); listItem.SetInnerText(errorText); htmlSummary.AppendLine(listItem.ToString(TagRenderMode.Normal)); } } } } 

The good news is that with open source MVC, you can go and grab the source from the CodePlex repository and configure it in some way how.

+6
source

Alternatively, you can reference this range via CSS and set it as p.

This is a way to link to it - you will need to adjust it accordingly:

 .validation-summary-errors > span { margin: 0px; } 
+2
source

Source: https://habr.com/ru/post/1310933/


All Articles