开发者

MVC3 html helpers in razorview

I have a custom ValidationSummary helper:

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开发者_JAVA技巧.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));
        }
    }
}

and i call it like this:

@Html.ValidationSummaryFor("test", new { @class = "test" })

however i keep getting this error:

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

I can't figure out what the problem is. anyone please help. thanks


You are passing in an anonymouse object (new { @class = "test" }) when your method has a parameter typed as IDictionary<string, object>.

So you either have to change the call site:

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

Or change the helper parameter

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

    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜