0
@ Html.ValidationSummaryの最初の5つのエラーのみを表示するビジネス要件があります。 ValidationSummaryLimited htmlhelperを作成しましたが、excludePropertyErrorsピースを実装する方法を理解できません。Custom @ 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();
}
このアウトをチェック役に立つかもしれませんhttp://stackoverflow.com/questions/14714290/how-to-display-only-one-error-message-using-html- validationsummary – David
私は[ソースコード](https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/ValidationExtensions.cs)を勉強することをお勧めします。アカウント。特に、次のように - あなたのケースでは、 'public static MvcHtmlString ValidationSummary(このHtmlHelper htmlHelper、bool excludePropertyErrors、文字列メッセージ、IDictionary <文字列、オブジェクト> htmlAttributes)'と 'private static IEnumerable GetModelStateList(HtmlHelper htmlHelper、bool excludePropertyErrors) 、あなたは2番目のメソッドの結果に '.Take(5)'を適用したいだけです。 –