Custom HTML hints what it is: htmlHelper html

I have two questions about creating custom HTML helpers.

1) Where should my method be located so that it can be used with @Html? I figured out how to use it with something like @MyCustomClass. Anyway, I will work. Just curious.

2) More importantly, what is the "HtmlHelper htmlHelper"? I notice that all built-in helpers, such as ActionLink, for example, start with this parameter, but then no value is passed to it. BUT ... when I try to create my own HTML helper, it seems to me that I need a value for this parameter or it gives me: "The best overload method contains more than x parameters."

I am trying to implement this example :

public static string IsSelected(this HtmlHelper html, string controllers = "", string actions = "", string cssClass = "selected")
{
    ...
}
+4
source share
1 answer

You can place your methods in the App_Code folder or in a special folder. I prefer to create a main folder from my application called \ Infrastructure. Under this, I created the “Views” folder and added the “ViewExtensions” class. Then, when I want to use it, I add "@using App.MVC.Infrastructure.Views" to the top of my view.

As for part 2, “this HtmlHelper htmlHelper” tells C # that you are creating an extension method for the HtmlHelper class. See https://msdn.microsoft.com/en-us/library/bb383977.aspx

http://develoq.net/2011/how-to-create-custom-html-helpers-for-asp-net-mvc-3-and-razor-view-engine/

+3

All Articles