MVC3 error Error creating HtmlButtonExtension

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

When I click this button, I want to pass the record identifier to my action

Below is what I added to razor mode

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

But I could not display this button, and it throws 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 someone help me determine the actual error

+2
source share
1 answer

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

You can add extra overload with object htmlAttributes . So 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()); } } 
+3
source

Source: https://habr.com/ru/post/1415311/


All Articles