2

これを使用して、ページにカスタムhtmlボタンを作成しようとしています。

public static class HtmlButtonExtension 
{
  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());
  }
}

このボタンをクリックすると、recordIDをアクションに渡します

以下に、かみそりのビューに追加したものを示します

@ Html.Button( "Delete"、new {name = "CustomButton"、recordID = "1"})

しかし、私はこのボタンを表示することができませんでした、そしてそれはエラーを投げています

'System.Web.Mvc.HtmlHelper<wmyWebRole.ViewModels.MyViewModel>' does not contain a definition for 'Button' and the best extension method overload 'JSONServiceRole.Utilities.HtmlButtonExtension.Button(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IDictionary<string,object>)' has some invalid arguments

誰かが実際のエラーを特定するのを手伝ってくれますか

4

1 に答える 1

3

IDictionary<string, object>forではなく匿名オブジェクトを渡していますhtmlAttributes

でオーバーロードを追加できますobject htmlAttributes。これは、組み込みのASP.NETMVCHtmlヘルパーでの方法です。

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());
  }

}
于 2012-05-04T22:38:19.620 に答える