Self-closing helpers are helpers that can contain HTML and Razor markup. The built-in @Html.BeginForm()
helper is an example of this helper type.
In order to create this type of HTML Helper, we'll need to create a helper class that implements the IDisposable
interface. Using the IDisposable
interface, we can write the element's closing tags when the object is disposed.
The Bootstrap Alert
component is a good candidate for such a helper. To create the helper, we'll have to complete the following steps:
Create a new subfolder called
Alerts
inside theHelpers
folder in your project.Open the
Enums.cs
file and add a new item calledAlertStyle
:public enum AlertStyle { Default, Primary, Success, Info, Warning, Danger }
Add a new class file called
Alert.cs
to theAlerts
folder.Add a new private, read-only
TextWriter
object field to the class called_writer
:private...