| Lesson 12 | Course Project Interactive Message |
| Objective | Put an interactive message into the course project. |
@code { } blocks of Razor (.razor) components.@page "/data"
Sales Tax Rate: @TaxRate
Product Name: @productName
@code {
private const decimal TaxRate = 0.07M; // constant
private string productName = "Gaming Keyboard"; // variable
private int quantity = 2;
private decimal price = 59.99M;
}
Subtotal: @(price * quantity):C
Tax: @((price * quantity) * TaxRate):C
Total: @((price * quantity) * (1 + TaxRate)):C
Total: @CalculateTotal():C
@code { private decimal CalculateTotal() { decimal subtotal = price * quantity; return subtotal + (subtotal * TaxRate); } }
@if (quantity > 5)
{
Bulk discount applies.
}
else
{
No discount.
}
is, typeof, or nullable checks:
@code {
private object data = "Hello Blazor";
private string TypeCheck()
{
if (data is string)
return "It's a string!";
else if (data is int)
return "It's an integer.";
return "Unknown type.";
}
}
@TypeCheck()
@if (price == 59.99M)
{
Price is set correctly.
}
@code {
private DateTime today = DateTime.Now;
private string upperName = "blazor".ToUpper();
private double sqrtValue = Math.Sqrt(25);
}
Today: @today.ToShortDateString()
Name Uppercase: @upperName
Square Root of 25: @sqrtValue
@code {
private string FormatProduct(string name, decimal price)
{
return $"{name} - {price:C}";
}
}
@FormatProduct("Webcam", 79.99M)
| Task | Syntax Example |
|---|---|
| Constant | private const int MaxQty = 10; |
| Variable | private string name = "Blazor"; |
| Total Calculation | subtotal = qty * price; |
| Branching (`if`, `else`) | if (qty > 5) { ... } |
| Looping (`foreach`, `for`) | foreach (var item in list) { ... } |
| Type Checking | if (obj is string) |
| Built-in Functions | DateTime.Now, Math.Sqrt(x), string.ToUpper() |
| User-defined Function | private string Format(...) { return ... } |