创建HtmlButtonExtension MVC3剃刀错误(MVC3 razor Error in

2019-09-17 01:19发布

我想用我的网页上创建自定义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所传递给我的行动

下面给出的是什么,我加入到我的Razor视图

@ Html.Button( “删除”,新的{名称= “的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

可有一个人帮我找出实际的错误

Answer 1:

你传递一个匿名对象,而不是一个IDictionary<string, object>htmlAttributes

您可以添加额外的超负荷object htmlAttributes 。 这是他们如何做到这一点在内置的ASP.NET MVC HTML辅助:

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

}


文章来源: MVC3 razor Error in creating HtmlButtonExtension