ASP.
NET MVC Razor
LECTURER : MR. HENG BORA, MITE
Contents
❑What is Razor?
❑Razor C# Syntax
❑Razor C# Variables & DataType
❑Razor C# Control Structure
❑Razor C# Arrays
❑Working With Objects
❑Razor with HTML Form
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 2
What is Razor?
➢ Razor is not a programming language. It's a server side
markup language.
➢ Razor is a markup syntax that lets you embed server-based
code (Visual Basic and C#) into web pages.
➢ Razor is an ASP.NET programming syntax used to create
dynamic web pages with the C# or VB.NET programming
languages.
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 3
Razor Syntax
➢ Razor supports both C# (C sharp) and VB (Visual Basic).
➢ Main Razor Syntax Rules for C#
✓ Razor code blocks are enclosed in @{ ... }
✓ Inline expressions (variables and functions) start with @
✓ Code statements end with semicolon
✓ Variables are declared with the var keyword
✓ Strings are enclosed with quotation marks
✓ C# code is case sensitive
✓ C# files have the extension .cshtml
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 4
C# Example
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 5
ASP.NET Razor - C# Variables
➢ Variables are declared using
the var keyword, or by using
the type (if you want to
declare the type), but
ASP.NET can usually
determine data types
automatically.
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 6
Data Types
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 7
Operators
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 8
Converting Data Types
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 9
The If Condition
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 10
The Else Condition
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 11
The Else If Condition
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 12
Switch Conditions
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 13
For Loops
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 14
While Loops
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 15
Arrays
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 16
Working With Objects
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 17
<!DOCTYPE html>
<html>
<head>
<title>Customer Form</title>
</head>
<body>
Razor with HTML Form
<form method="post" >
<fieldset>
<legend>Add Customer</legend>
<div>
<label for="CompanyName">Company Name:</label>
<input type="text" name="CompanyName" value="" />
</div>
<div>
<label for="ContactName">Contact Name:</label>
<input type="text" name="ContactName" value="" />
</div>
<div>
<label for="Employees">Employee Count:</label>
<input type="text" name="Employees" value="" />
</div>
<div>
<label> </label>
<input type="submit" value="Submit" class="submit" />
</div>
</fieldset>
</form>
</body>
</html>
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 18
Reading User Input from the Form
@{
if (IsPost) {
string companyname = Request.Form["companyname"];
string contactname = Request.Form["contactname"];
int employeecount = Request.Form["employees"].AsInt();
<text>
You entered: <br />
Company Name: @companyname <br />
Contact Name: @contactname <br />
Employee Count: @employeecount <br />
</text>
}
}
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 19
What is the Model in MVC
❖ In MVC M stands for Model and Model is a normal C# class.
❖ Model is responsible for handling data and business logic.
❖ A model represents the shape of the data.
❖ Model is responsible for
handling database related
changes
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 20
Adding a Model
1. Right-click on the Model folder
2. select Add -> and click on Class...
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 21
Integrate Controller, View and Model
❑ we will integrate Controller, model and view to run the application and see the result.
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 22
12/20/2022 WEB DEVELOPMENT WITH ASP.NET 23