HTML Helpers
HTML Helpers
NET MVC
HTML Helpers are methods in ASP.NET MVC that generate HTML elements dynamically in a
view. These helpers simplify the creation of form elements and other HTML controls by
providing a strongly typed and server-side way to build the HTML.
1. Standard Helpers
Description: These helpers generate HTML elements but are not tied to model
properties. They are less type-safe and require manual management of field names.
Examples:
// Controller
public ActionResult Index()
{
ViewBag.Name = "John Doe";
return View();
}
// View
@Html.TextBox("Name", ViewBag.Name, new { @class = "form-control",
placeholder = "Enter your name" })
Description: These helpers are tied to the model's properties, ensuring type safety. They
reduce runtime errors and are recommended for strongly typed views.
Examples:
// Model
public class UserModel
{
public string Name { get; set; }
}
// Controller
public ActionResult Index()
{
var model = new UserModel { Name = "John Doe" };
return View(model);
}
// Model
public class UserModel
{
public string Country { get; set; }
public List<SelectListItem> Countries { get; set; }
}
// Controller
public ActionResult Index()
{
var model = new UserModel
{
Countries = new List<SelectListItem>
{
new SelectListItem { Text = "USA", Value = "USA" },
new SelectListItem { Text = "UK", Value = "UK" },
new SelectListItem { Text = "Canada", Value = "Canada" }
}
};
return View(model);
}
// View
@model UserModel
@Html.DropDownListFor(m => m.Country, Model.Countries, "Select a
Country", new { @class = "form-select" })
3. Custom Helpers
// View
@Html.CustomButton("Submit", "submit", "btn btn-success")
// Model
public class UserModel
{
public string Country { get; set; }
public List<SelectListItem> Countries { get; set; }
}
// Controller
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new UserModel
{
Countries = new List<SelectListItem>
{
new SelectListItem { Text = "USA", Value = "USA" },
new SelectListItem { Text = "UK", Value = "UK" },
new SelectListItem { Text = "Canada", Value =
"Canada" }
}
};
return View(model);
}
}
// View (Index.cshtml)
@model UserModel
@Html.DropDownListFor(m => m.Country, Model.Countries, "Select a
Country", new { @class = "form-select" })
// Controller
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
// View (Index.cshtml)
@Html.CustomButton("Click Me", "button", "btn btn-primary")
// Controller
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
// View (Index.cshtml)
@Html.CustomLabel("Name:", "form-label")
Strongly Typed HTML Helpers in ASP.NET MVC
Strongly Typed HTML Helpers in ASP.NET MVC are designed to provide type safety and
IntelliSense for creating form fields and UI elements in a view. These helpers are bound to a
specific model, ensuring that field names and values correspond to properties of the model.
namespace YourNamespace.Models
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public bool IsActive { get; set; }
public string Gender { get; set; }
}
}
using System.Web.Mvc;
using YourNamespace.Models;
namespace YourNamespace.Controllers
{
public class UserController : Controller
{
public ActionResult Create()
{
return View(new User());
}
[HttpPost]
public ActionResult Create(User user)
{
if (ModelState.IsValid)
{
// Save the user to the database (example purpose)
return RedirectToAction("Details", new { id =
user.Id });
}
return View(user);
}
}
}
@model YourNamespace.Models.User
<!DOCTYPE html>
<html>
<head>
<title>Create User</title>
</head>
<body>
<h1>Create User</h1>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
</div>
<div>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
</div>
<div>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</div>
<div>
@Html.LabelFor(m => m.IsActive)
@Html.CheckBoxFor(m => m.IsActive)
</div>
<div>
<label>Gender</label>
@Html.RadioButtonFor(m => m.Gender, "Male") Male
@Html.RadioButtonFor(m => m.Gender, "Female") Female
</div>
<button type="submit">Submit</button>
}
</body>
</html>
namespace YourNamespace.Models
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public bool IsActive { get; set; }
public string Gender { get; set; }
public string Country { get; set; }
}
}
using System.Web.Mvc;
using YourNamespace.Models;
namespace YourNamespace.Controllers
{
public class UserController : Controller
{
public ActionResult Create()
{
return View(new User());
}
[HttpPost]
public ActionResult Create(User user)
{
if (ModelState.IsValid)
{
// Logic to save the user (e.g., database)
return RedirectToAction("Details", new { id =
user.Id });
}
return View(user);
}
}
}
@model YourNamespace.Models.User
<!DOCTYPE html>
<html>
<head>
<title>Create User</title>
</head>
<body>
<h1>Create User</h1>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
</div>
<div>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
</div>
<div>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</div>
<div>
@Html.LabelFor(m => m.IsActive)
@Html.CheckBoxFor(m => m.IsActive)
</div>
<div>
<label>Gender</label>
@Html.RadioButtonFor(m => m.Gender, "Male") Male
@Html.RadioButtonFor(m => m.Gender, "Female") Female
</div>
<div>
@Html.LabelFor(m => m.Country)
@Html.DropDownListFor(m => m.Country, new
SelectList(new[] { "USA", "UK", "Canada" }))
</div>
<button type="submit">Submit</button>
}
</body>
</html>