In this article you will learn the followings things,
- What is Html Helper in Asp.Net MVC and types?
- Why do we need custom Html Helper?
- How to create Custom Html Helper?
- Step by Step Implementation
What is HTML Helper Asp.Net MVC and its types ?
HtmlHelper is a class inside namespace System.Web.Mvc. This class handles and is responsible to generate HTML tag in ViewEngine.
There are two types of HTML Helpers:
- Non-Strongly Typed Html Helper (Basic Html Helper)
- Strongly Typed Html Helper
Example
@Html.Label ---- Non-Strongly Typed Html Helper Example
@Html.LabelFor ---- Strongly Typed Html Helper Example
Why do we need custom Html Helper?
You can design your own html helper as per your requirement; those html helpers are called Custom Html Helper.
How to create Custom Html Helper?
Custom creation of Html Helper is very simple. We have to create only class file with Static Class and Static Method.
Example
- public static class CustomerHelper
- {
- public static IHtmlString LabelRequired(this HtmlHelper helper , string LabelText, string Symbol, string HtmlClass)
- {
- }
- }
Create a new project called “AspNetMvc”
In the below dialog box do the followings,
SR.
NO.
|
DESCRIPTION
|
1
|
Select MVC Template.
|
2
|
Confirm or Check MVC check box selected .
|
3
|
Click Change Authentication button select NO AUTHENTICATION value.
|
4
|
Click OK button to proceed ahead.
|
Visual Studio will create the project as per the above selections.
Default view of SOLUTION EXPLORER.
Add new class called “CustomHelper”, and now right click on Project ADD-->CLASS
While you try HtmlHelper in Parameter section it shows a RED UNERLINE.
This is because HTML HELPER is using the namespace System.Web.Mvc.
CustomHelper.cs Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace AspNetMvc
- {
- public static class CustomHelper
- {
-
-
-
-
-
-
-
-
- public static IHtmlString LabelRequired(this HtmlHelper helper, string DisplayText, string Symbol, string HtmlClass)
- {
- string LabelRequiredString = string.Empty;
-
-
- TagBuilder tb = new TagBuilder("label");
-
-
- tb.AddCssClass(HtmlClass);
-
-
- tb.InnerHtml = DisplayText + "<span style='color:red;font-size:20px;'>" + Symbol + "</span>";
-
- return new MvcHtmlString(tb.ToString());
- }
- }
- }
Now switch to Models folder, right click on Model folder and add new model called “MemberModel”
MemberModel.cs code
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
-
- namespace AspNetMvc.Models
- {
- public class MemberModel
- {
- [Required(ErrorMessage ="Full name required")]
- public string FullName { get; set; }
-
- public string Address { get; set; }
-
- public string Phone { get; set; }
-
- [Required(ErrorMessage = "Email required")]
- public string Email { get; set; }
-
- }
- }
Now switch to Controllers folder, double click on HomeController and add new ActionResult called NewMember()
Default Code is generated using Scaffolding,
- @model AspNetMvc.Models.MemberModel
- @{
- ViewBag.Title = "NewMember";
- }
- <h2>NewMember</h2>
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
-
- <div class="form-horizontal">
- <h4>MemberModel</h4>
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- @Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
-
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
-
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
After using Customer Helper,
Example
- @Html.LabelRequired("Full Name", "*", "control-label col-md-2")
NewMember.cshtml code
- @model AspNetMvc.Models.MemberModel
- @{
- ViewBag.Title = "NewMember";
- }
-
- <h2>NewMember</h2>
-
- @using AspNetMvc
-
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
-
- <div class="form-horizontal">
- <h4>MemberModel</h4>
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- @Html.LabelRequired("Full Name", "*", "control-label col-md-2")
- <div class="col-md-10">
-
- @Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelRequired("Email", "*", "control-label col-md-2")
- <div class="col-md-10">
- @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
-
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
-
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
OUTPUT
Happy Coding.