且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在创造HtmlButtonExtension MVC3剃须刀错误

更新时间:2023-02-14 18:33:43

您正在传递一个匿名对象,而不是一个的IDictionary<字符串对象> htmlAttributes

You're passing an anonymous object, not an IDictionary<string, object> for htmlAttributes.

您可以添加使用对象htmlAttributes 额外的过载。这是他们如何做到在内置的ASP.NET MVC HTML辅助:

You can add an additional overload with object htmlAttributes. This is how they do it in the built-in ASP.NET MVC Html Helpers:

public static class HtmlButtonExtension 
{    
  public static MvcHtmlString Button(this HtmlHelper helper, string text,
                                     object htmlAttributes)
  {
      return Button(helper, text, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
  }

  public static MvcHtmlString Button(this HtmlHelper helper, string text,
                                     IDictionary<string, object> htmlAttributes)
  {
      var builder = new TagBuilder("button");
      builder.InnerHtml = text;
      builder.MergeAttributes(htmlAttributes);
      return MvcHtmlString.Create(builder.ToString());
  }

}