我有一个自定义的ValidationSummary帮助程序:
namespace System.Web.Mvc.Html
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
public static class ValidationExtensions
{
public static MvcHtmlString ValidationSummaryFor(this HtmlHelper htmlHelper,
string message,
IDictionary<string, object> htmlAttributes)
{
if (htmlHelper.ViewData.ModelState.IsValid)
{
return null;
}
var l = htmlHelper.ViewData.ModelState.Where(e => e.Value.Errors.Count != 0).ToList();
// Nothing to do if there aren't any errors
if (l.Count() == 0)
{
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 (KeyValuePair<string, ModelState> keyValuePair in l)
{
foreach (ModelError modelError in keyValuePair.Value.Errors)
{
var errorText = modelError.ErrorMessage;
if (!String.IsNullOrEmpty(errorText))
{
TagBuilder listItem = new TagBuilder("li");
listItem.SetInnerText(errorText);
htmlSummary.AppendLine(listItem.ToString(TagRenderMode.Normal));
}
}
}
unorderedList.InnerHtml = htmlSummary.ToString();
return MvcHtmlString.Create(messageSpan + unorderedList.ToString(TagRenderMode.Normal));
}
}
}
我这样调用它:
@Html.ValidationSummaryFor("test", new { @class = "test" })
但是我一直遇到这个错误:
CS1928: 'System.Web.Mvc.HtmlHelper<Web.Areas.Admin.Controllers.LogOnViewModel>' does not contain a definition for 'ValidationSummaryFor' and the best extension method overload 'System.Web.Mvc.Html.ValidationExtensions.ValidationSummaryFor(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IDictionary<string,object>)' has some invalid arguments
我无法确定问题出在哪里,请有人帮忙。谢谢。