2016-05-19 14 views
0

Ich habe eine Geschäftsanforderung, nur die ersten 5 Fehler in der @ Html.ValidationSummary anzuzeigen. Ich habe einen ValidationSummaryLimited HTMLHelper erstellt, aber ich kann nicht herausfinden, wie man die excludePropertyErrors Komponente implementiert.Benutzerdefiniert @ Html.ValidationSummary excludePropertyErrors

public static string ValidationSummaryLimited(this HtmlHelper helper, bool excludePropertyErrors = false, string message = "") 
{ 
    const int maximumValidations = 5; 

    if (helper.ViewData.ModelState.IsValid) 
     return string.Empty; 

    StringBuilder sb = new StringBuilder(); 

    if (!string.IsNullOrEmpty(message)) 
    { 
     sb.AppendFormat("<span>{0}</span>{1}", helper.Encode(message), System.Environment.NewLine); 
    } 

    sb.AppendLine("<div class=\"validation-summary-errors\" data-valmsg-summary=\"true\">"); 
    sb.AppendLine("\t<ul>"); 

    int count = 0; 
    foreach (var key in helper.ViewData.ModelState.Keys) 
    { 
     foreach (var err in helper.ViewData.ModelState[key].Errors) 
     { 
      count++; 
      sb.AppendFormat("\t\t<li>{0}</li>{1}", helper.Encode(err.ErrorMessage),System.Environment.NewLine); 

      if (count >= RVConstants.MaximumValidationErrors) 
      { 
       sb.AppendFormat("\t\t<li>Maximum of {0} errors shown</li>{1}", 
        maximumValidations, 
        System.Environment.NewLine); 
       break; 
      } 
     } 

     if (count >= maximumValidations) 
      break; 
    } 

    return sb.ToString(); 
} 
+0

Check this out könnte hilfreich sein http://stackoverflow.com/questions/14714290/how-to-display-only-one-error-message-using-html- validationsummary – David

+0

Ich empfehle Ihnen, den [Quellcode] zu studieren (https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/ValidationExtensions.cs) - es gibt eine Menge, die Sie nicht nehmen Konto. Insbesondere das folgende - 'öffentliche statische MvcHtmlString ValidationSummary (diese HtmlHelper htmlHelper, bool excludePropertyErrors, string Nachricht, IDictionary htmlAttributes)' und 'private statische IEnumerable GetModelStateList (HtmlHelper htmlHelper, bool excludePropertyErrors)' - in Ihrem Fall , Ihr Wille will nur ein '.Take (5)' angewendet auf das Ergebnis der 2. Methode. –

Antwort

0

Ich nutzte die vorhandene Validierungszusammenfassung. Hier ist, was ich kam mit:

public static MvcHtmlString ValidationSummaryLimited(this HtmlHelper helper, 
    bool excludePropertyErrors) 
{ 
    MvcHtmlString result = helper.ValidationSummary(excludePropertyErrors); 
    return LimitSummaryResults(result); 
} 

private static MvcHtmlString LimitSummaryResults(MvcHtmlString summary) 
{ 
    if (summary == null || summary.ToString() == string.Empty) 
     return summary; 

    int maximumValidations = RVConstants.MaximumValidationErrors; 

    Regex exp = new Regex(@"<li[^>]*>[^<]*</li>",RegexOptions.IgnoreCase); 

    string htmlString = summary.ToHtmlString(); 
    MatchCollection matches = exp.Matches(htmlString); 

    if (matches.Count > maximumValidations) 
    { 
     string header = htmlString.Substring(0, htmlString.IndexOf("<ul>",StringComparison.Ordinal) + "<ul>".Length); 
     string footer = htmlString.Substring(htmlString.IndexOf("</ul>", StringComparison.Ordinal)); 

     StringBuilder sb = new StringBuilder(htmlString.Length); 
     sb.Append(header); 

     for (int i = 0; i < maximumValidations; i++) 
     { 
      sb.Append(matches[i].Value); 
     } 

     sb.AppendFormat("<li>Maximum of {0} errors shown</li>", maximumValidations); 

     sb.Append(footer); 

     string limited = sb.ToString(); 
     return new MvcHtmlString(limited); 
    } 

    return summary; 
} 
Verwandte Themen