0% found this document useful (0 votes)
8 views5 pages

HTML Helper Functions in ASP.NET MVC

The document provides an overview of HTML Helper Functions in ASP.NET, explaining their types and benefits, particularly focusing on strongly typed helpers. It also discusses scaffolding for generating controllers and views, validation using data annotations, and the use of ViewBag for passing data from controllers to views. Additionally, it touches on CRUD operations and introduces ADO.NET for data management.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

HTML Helper Functions in ASP.NET MVC

The document provides an overview of HTML Helper Functions in ASP.NET, explaining their types and benefits, particularly focusing on strongly typed helpers. It also discusses scaffolding for generating controllers and views, validation using data annotations, and the use of ViewBag for passing data from controllers to views. Additionally, it touches on CRUD operations and introduces ADO.NET for data management.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MS .

NET Notes Day-8 (21 December, 2025)


HTML Helper Functions
What are HTML Helper Functions?

HTML Helpers are server-side methods used in Razor views to generate HTML elements dynamically.
They:
Reduce manual HTML coding
Support model binding
Integrate validation automatically

Types of HTML Helpers

Strongly Typed HTML Helpers (Recommended)

Work with model properties and provide compile-time safety.


Code Example:

@[Link](m => [Link])


@[Link](m => [Link])
@[Link](m => [Link])

Weakly Typed HTML Helpers

Do not provide compile-time checking.


Code Example:

@[Link]("Name")
@[Link]("Age")

Common HTML Helpers

Helper Purpose

TextBoxFor Input textbox

PasswordFor Password input

CheckBoxFor Checkbox

RadioButtonFor Radio button

DropDownListFor Dropdown

ValidationSummary Validation messages


Creating Strongly Typed Views
What Is a Strongly Typed View?

A view that is bound to a specific model type.

Syntax:

@model Student //While desiging a view we are binding to it Student model

Benefits

IntelliSense support
Compile-time checking
Easy model binding
Cleaner code
Code Example:

@model Student

<label asp-for="Name"></label>
<input asp-for="Name" />
<span asp-validation-for="Name"></span>

Using Various Scaffold Templates


What Is Scaffolding?

Scaffolding automatically generates:

Controllers
Views
CRUD logic

Based on a model class e.g. Student, Teacher etc.

Scaffold Templates in Visual Studio

Template Use

MVC Controller with views Full CRUD

MVC Controller – Empty Custom logic

Razor Pages Page-based approach

API Controller REST APIs


How to Scaffold CRUD

Right-click Controllers
Add → Controller
Select MVC Controller with views, using Entity Framework
Choose Model and DbContext (Will discuss tommorow)
Click Add

Validation Using Data Annotations


What Are Data Annotations?

Data Annotations are attributes used on model properties to define:


Validation rules
Display metadata
Common Data Annotation Attributes
Code Example:

using [Link];

public class Student


{
public int Id { get; set; }

[Required(ErrorMessage = "Name is mandatory")]


[StringLength(50)]
public string Name { get; set; }

[Range(5, 60)]
public int Age { get; set; }

[EmailAddress]
public string Email { get; set; }
}

Client-Side Validation

How It Works?

Uses jQuery Unobtrusive Validation


Validates input before form submission
Improves user experience
Required Scripts

<script src="~/lib/jquery/[Link]"></script>
<script src="~/lib/jquery-validation/[Link]"></script>
<script src="~/lib/jquery-validation-
unobtrusive/[Link]"></script>
Server-Side Validation

We write server check logic in Controller Code


Code Example:

[HttpPost]
public IActionResult Create(Student student)
{
if ([Link])
{
// Save data
return RedirectToAction("Index");
}
return View(student);
}

Why Server-Side Validation Is Mandatory

Client-side can be bypassed


Ensures security and data integrity

Understanding ViewBag
What is ViewBag?

ViewBag is a dynamic object used to pass data from Controller to View.


Characteristics
Dynamic (no compile-time checking)
Uses ViewData internally
Controller → View only
Passing Data Using ViewBag
Code Example:

//StudentController
public IActionResult Index()
{
[Link] = "Welcome to MVC";
[Link] = 50;
return View();
}

Creating a View Using ViewBag


View ([Link])
<h2>@[Link]</h2>
<p>Total Students: @[Link]</p>

Limitations of ViewBag

No IntelliSense
Runtime errors possible
Not suitable for complex data

CRUD operation using Model (Kindly examine the today's project)


To be discussed tomorrow (22 Dec, 2025)
Self validated model

Adding Service and DAO layer to today's project

Data Management with [Link]


[Link] introduction

Connection object, Command object, DataReader, DataAdapter, DataSet and DataTable

Asynchronous command Execution

Asynchronous Connections

You might also like