If we want our helpers to behave in a manner similar to the built-in ASP.NET MVC HTML Helpers, we need to create an extension method. Extension methods are a technique used to add new methods to an existing class.
Note
Extension methods are a very powerful and intuitive approach to add additional functionality to existing classes and can help you in many ways. You can read more about extension methods on MSDN at http://bit.ly/ExtMethods.
We'll create an extension method to the HtmlHelper
class that is represented by a view's HTML property by completing the following steps:
Start by adding a new class file called
ButtonExtensions.cs
to theHelpers
folder in the root of your project.Change the class type to
static
. Extension methods need to be declared inside a static class.Add a new method called
BootstrapButton
to the class. The first parameter of the method indicates which class the extension extends and must be preceded with thethis
keyword.The remaining...