我正在尝试使用 this 在我的页面上创建自定义 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 View 中的内容
@Html.Button("删除", 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
有人可以帮我找出实际的错误吗
最佳答案
您正在传递一个匿名对象,而不是 IDictionary<string, object>
对于 htmlAttributes
.
您可以使用 object htmlAttributes
添加额外的重载。这是他们在内置 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());
}
}
关于asp.net-mvc-3 - MVC3 razor 创建 HtmlButtonExtension 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10456956/