在创造HtmlButtonExtension MVC3剃须刀错误 [英] MVC3 razor Error in creating HtmlButtonExtension
问题描述
我试图使用此创建我的网页上的自定义HTML按钮
I am trying to create a custom html button on my page using this
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所传递给我的动作
When I click this button I want to pass an recordID to my Action
下面给出的是什么,我加入到我的Razor视图
Given below is what I added to my razor view
@ Html.Button(删除,新名称{=的CustomButton的recordId =1})
@Html.Button("Delete", new {name="CustomButton", recordID ="1" })
但我不能得到显示这个按钮,它的投掷误差修改
But I could not get to display this button,and it's throwing erros
'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
有人可以帮我找出实际误差
Can some one help me to identify the actual error
推荐答案
您正在传递一个匿名对象,而不是一个的IDictionary&LT;字符串对象&gt;
为 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());
}
}
这篇关于在创造HtmlButtonExtension MVC3剃须刀错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!