I have two questions about creating custom HTML Helpers.
1) Where would my method be placed so that it can be used with @Html? I've figured out how use it with something like @MyCustomClass. Either will work, I suppose. Just curious.
2) More importantly, what is "this HtmlHelper htmlHelper"? I notice all the built in helpers, such as ActionLink for example, start with this parameter, but then no value is actually passed in for it. BUT... when I try to create my own custom HTML helper, it seems to want 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")
{
...
}
You can place your methods in the App_Code folder or a custom folder. I prefer to create a main folder off my app called \Infrastructure. Under that I created a "Views" folder and added a "ViewExtensions" class. Then when I want to use it I add a "@using App.MVC.Infrastructure.Views" to the top of my view.
As to part 2, "this HtmlHelper htmlHelper" tells C# that you are creating an extension method for the class HtmlHelper. 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/