0% found this document useful (0 votes)
35 views22 pages

Advance Web Application Development: Instructor: Dr. Syed Ali Raza Department of Computer Science GC University Lahore

The document provides an overview of working with Razor Pages in ASP.NET Core, including implicit and explicit Razor expressions, passing data between controllers and views using ViewBag, ViewData, and TempData, variables, data types, operators, control structures like if/else, switch, foreach, for, while, and do-while loops. It describes how to render values, perform logic, and iterate over collections in Razor Pages views.

Uploaded by

Mishi Baba
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)
35 views22 pages

Advance Web Application Development: Instructor: Dr. Syed Ali Raza Department of Computer Science GC University Lahore

The document provides an overview of working with Razor Pages in ASP.NET Core, including implicit and explicit Razor expressions, passing data between controllers and views using ViewBag, ViewData, and TempData, variables, data types, operators, control structures like if/else, switch, foreach, for, while, and do-while loops. It describes how to render values, perform logic, and iterate over collections in Razor Pages views.

Uploaded by

Mishi Baba
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
You are on page 1/ 22

Advance Web Application

Development
Instructor: Dr. Syed Ali Raza
Department of Computer Science
GC University Lahore
Give someone a program, you frustrate them for a day;
teach them how to program, you frustrate them for a
lifetime.

— David Leinweber
Lecture 8
Working with Razor Pages
Implicit Razor Expressions

• Implicit Razor expressions start with @ followed by C# code:


<p>@DateTime.Now</p>
<p>@DateTime.IsLeapYear(2016)</p>

• With the exception of the C# await keyword, implicit expressions


must not contain spaces.
<p>@await DoSomething("hello", "world")</p>
Explicit Razor Expressions

• Explicit Razor expressions consist of an @ symbol with balanced parenthesis.

• For example, to render last week's time, the following Razor markup is used:
<p>Last week this time: @(DateTime.Now -TimeSpan.FromDays(7))
</p>

• Any content within the @() parenthesis is evaluated and rendered to the output.

• Implicit expressions, described in the previous section, generally can't contain


spaces. In the following code, one week isn't subtracted from the current time:
<p>Last week: @DateTime.Now - TimeSpan.FromDays(7)</p>

• The code renders the following HTML:


<p>Last week: 7/7/2016 4:39:52 PM - TimeSpan.FromDays(7)</p>
Passing Data between Controllers and Views

• In ASP.NET MVC, developers have several options for passing data


between controllers and views
• ViewBag
• ViewData
• TempData.
• These mechanisms enable communication between different
components of the MVC architecture.
• Overall, ViewBag, ViewData, and TempData serve similar purposes of
passing data between controllers and views in ASP.NET MVC.
• Developers can choose the appropriate mechanism based on their
specific requirements and preferences. However, it's important to
note that these mechanisms are primarily intended for passing small
amounts of data and may not be suitable for complex data sharing
scenarios. For those cases, using strongly-typed models is often a
better approach.
Passing Data between Controllers and Views

• ViewBag:
• ViewBag is a dynamic property that enables developers to pass data from
controllers to views.
• It is a wrapper around ViewData, making it easier to work with dynamic
properties in C#.
• Dynamic properties in C# refer to properties that are resolved at runtime rather than at
compile-time.
• Developers can assign values to ViewBag properties in controllers, and then
access those values in corresponding views.
public class HomeController : Controller <head>
{ <title>ViewBag Example</title>
public IActionResult Index() </head>
{ <body>
ViewBag.Message = "Hello, world!"; <h1>@ViewBag.Message</h1>
return View(); </body>
}
}
Passing Data between Controllers and Views

• ViewData
• ViewData is a dictionary-like container that allows passing data from
controllers to views.
• It uses a key-value pair approach to store data.
• Unlike ViewBag, type casting can be applied when accessing its values in
views using ViewData i.e, @((string)ViewData["Message"])

public class HomeController : Controller <head>


{ <title>ViewData Example</title>
public IActionResult Index() </head>
{ <body>
ViewData["Message"] = "Hello, world!"; <h1>@ViewData["Message"]</h1>
ViewData["Count"] = 10;return View(); <p>Count: @ViewData["Count"]</p>
} </body>
}
Passing Data between Controllers and Views

• TempData
• TempData is similar to ViewData, but it is specifically designed for passing
data between controller actions and redirects.
• Data stored in TempData persists for the duration of the current HTTP
request and the subsequent request only. After that, the data is removed
automatically.
• It is commonly used for passing messages or flags between actions during
redirects.
• In the TempData example, we use it to pass data from the Index action to the
DisplayMessage action through a redirect.
• The TempData dictionary preserves data for one subsequent request only,
which makes it suitable for scenarios involving redirects.
Variables

• Variables are used to store data.


• The name of a variable must begin with an alphabetic character and
cannot contain whitespace or reserved characters.

A variable can be of a specific type, indicating the kind of data it


stores. String variables store string values ("Welcome to MVC"),
integer variables store number values (103), date variables store date
values, etc.

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.
Data Types

Type Description Examples

int Integer (whole numbers) 103, 12, 5168

float Floating-point number 3.14, 3.4e38

Decimal number (higher


decimal 1037.196543
precision)

bool Boolean true, false

string String "Hello MVC", “Ali"


Operators

• An operator tells ASP.NET what kind of command to perform in an


expression. The C# language supports many operators.
Operator Description Example
= Assigns a value to a variable. i=6
+
Adds a value or variable. i=5+5
-
Subtracts a value or variable. i=5-5
*
Multiplies a value or variable. i=5*5
/
Divides a value or variable. i=5/5
+= / ++ Increments a variable. i += 1
-= / - - Decrements a variable. i -= 1
== Equality. Returns true if values are equal. if (i==10)
!= Inequality. Returns true if values are not equal. if (i!=10)
< Less than. if (i<10)
> Greater than. if (i>10)
<= Less than or equal. if (i<=10)
>= Greater than or equal. if (i>=10)
+ Adding strings (concatenation). “Hello" + “MVC"
. Dot. Separate objects and methods. DateTime.Hour
() Parenthesis. Groups values. (i+5)
() Parenthesis. Passes parameters. x=Add(i,5)
[] Brackets. Accesses values in arrays or collections. name[3]
! Not. Reverses true or false. if (!ready)
&& Logical AND. if (ready && clear)
|| Logical OR. if (ready || clear)
Control Structures: IF Statement

• Control structures are an extension of code blocks. All aspects of


code blocks (transitioning to markup, inline C#) also apply to the
following structures.
• Conditionals
• @if,
• else if, else
• @switch
• The @if family controls when code runs:

@if (value % 2 == 0)
{
<p>The value was even</p>
}
Control Structures: IF-Else Statement

• else and else if don’t require the @ symbol:


@if (value % 2 == 0)
{
<p>The value was even</p>
}
else if (value >= 1000)
{
<p>The value is large.</p>
}
else
{
<p>The value was not large and is
odd.</p>
}
Control Structures: Switch Statement

• Switch statement
@switch (value)
{
case 1:
<p>The value is 1!</p>
break;
case 1337:
<p>Your number is 1337!</p>
break;
default:
<p>Your number was not 1 or
1337.</p>
break;
}
Control Structures: Looping

• Razorpages supports following loop structures


• @foreach
• @for
• @while
• @do while
Control Structures: Foreach Loop

• the foreach loop is a construct used to iterate over collections or


arrays, allowing you to perform an action on each element of the
collection. It simplifies the process of iterating through elements
without needing to explicitly manage the loop's index or counter
variable.

@foreach (var person in people)


{
}
Control Structures: For Loop

• The for loop is used when you know the number of iterations in
advance or need to iterate over a sequence of numbers.
• It has a more explicit syntax that includes initialization, condition,
and increment/decrement sections.
• It is commonly used when you need precise control over loop
iterations or need to iterate over a sequence of numbers.

@for (var i = 0; i < people.Length; i++)


{
var person = people[i];

}
Control Structures: While Loop

• The while loop is used when you want to execute a block of code
repeatedly based on a condition.
• It evaluates the condition before each iteration and continues as long
as the condition remains true.
• It is suitable when the number of iterations is not known in advance
and depends on a condition.

@{ var i = 0; }
@while (i < people.Length)
{
var person = people[i];
i++;
}
Control Structures: Do-While Loop

• The do-while loop is similar to the while loop, but it always executes
the block of code at least once before evaluating the condition.
• It is suitable when you want to ensure that the loop body executes at
least once, regardless of the condition.
@{ var i = 0; }
@do
{
var person = people[i];
i++;
} while (i < people.Length);
Control Structures Exercise

@{ var days = new int[] { 7, 14, 21, 28, 35 }; }


@foreach (var date in days) {
<li> @(DateTime.Now + TimeSpan.FromDays(date)) </li>
}
@{
var d = DateTime.Now.DayOfWeek.ToString();
<div>
@d
@if (d == "Thursday")
{
<div>abc</div>
}
else
{
<div>xyz</div>
}
</div>
}
Control Structures Exercise

@{
var c = (DateTime.Now.Date + TimeSpan.FromDays(7));

var a = @c.ToString();
<div>
@if(a.Contains("12/10/2021"))
{
<div>abc</div>
}
else
{
<div>xyz</div>
}
</div>
}

You might also like