As Bootstrap offers a variety of different components, it lends itself perfectly towards the use of helpers. The simplest form of helper in ASP.NET MVC is the one that you can create using the @helper
syntax. A custom helper can include any HTML or even Razor markup. To create a simple helper method to generate a Bootstrap button, complete the following steps:
Create a new folder called
App_Code
inside the root folder of your website.Create a new file called
BootstrapHelpers.cshtml
inside the newly createdApp_Code
folder.Add the following code to the
BootstrapHelper.cshtml
file:@helper PrimaryButtonSmall(string id,string caption) { <button id="@id" type="button" class="btn btn-primary btn-sm">@caption</button> }
This code employs the
@helper
syntax to create a new helper calledPrimaryButtonSmall
. The helper accepts two parameters in order to specify theid
attribute of thebutton
element as well as the button'scaption
element. We've added appropriate...