Advance Web Application Development: Instructor: Dr. Syed Ali Raza Department of Computer Science GC University Lahore
Advance Web Application Development: Instructor: Dr. Syed Ali Raza Department of Computer Science GC University Lahore
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
• 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.
• 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"])
• 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 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
@if (value % 2 == 0)
{
<p>The value was even</p>
}
Control Structures: IF-Else 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
• 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.
}
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 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>
}