剃刀视图中的MVC3 html助手

时间:2011-01-25 22:53:22

标签: asp.net-mvc-3

我有一个自定义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

我无法弄清问题是什么。有人请帮忙。 感谢

1 个答案:

答案 0 :(得分:3)

当您的方法的参数类型为new { @class = "test" }时,您传入的是匿名对象(IDictionary<string, object>)。

所以你要么改变呼叫站点:

@Html.ValidationSummaryFor("test", new Dictionary<string, object>() { {"class", "test"} })

或更改辅助参数

ValidationSummary(HtmlHelper helper, string s, object o) {
    var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(o);

    ...
}