What is an HTML Helper?
It's a piece of Razor code that you can call from multiple locations on your page.
Here's an example:
Create this at the top of your page.
@helper ShowWarning(ByVal warningText As String) <span class="warning"> @warningText </span> End Helper
To reference this from anywhere within the page.
@ShowWarning("Oh No! There has been an error")
If you feel that this code will be useful enough to use across all your views you can create a global helper. To do this:
- Right click on the app_code folder
- Select "Add > New Item"
- choose "helper" under the Razor templates
- Give the file a name. Let's call ours "dwHelper".
- and put in the helper code above.
You have now made your helper globally accessible.
You will need to make a slight change to your code that calls the helpers now:
@dwHelper.ShowWarning("Oh No! There has been an error")