aspnet
  1. aspnet-razor-control-structures

Razor Control Structures

Razor Control Structures are used to apply conditional logic and loops in the Razor view pages. Control Structures are used to create dynamic content when the page is rendered. You can use these structures to add functionalities or perform the check for null values before rendering the view.

Syntax

The Razor Control Structures can be added in the Razor view by using the @ symbol followed by the Razor Code Block that contains the control structures. Here is the syntax for Razor Control Structures.

@if (condition)
{
    // code to execute if condition is true
}
@switch (variable)
{
    case value1:
        // code to execute if the variable is equal to value1
        break;
    case value2:
        // code to execute if the variable is equal to value2
        break;
    default:
        // code to execute if none of the above cases satisfy
        break;
}
@foreach (var item in items)
{
    // code to execute for each item in items
}
@while (condition)
{
    // code to execute until the condition is false
}

Example

Let's take a simple example to understand how Razor Control Structures work in ASP.NET Razor.

@if (Model != null)
{
    <ul>
        @foreach (var item in Model)
        {
            <li>@item.Name</li>
        }
    </ul>
}
else
{
    <p>No items found</p>
}

In the above example, if the Model is not null, then a list of items will be created using a foreach loop. If the Model is null, then the message "No items found" will be displayed.

Output

The output of the Razor Control Structures will vary based on the conditional statement or loop used. In the above example, if the Model is not null, then the output will be a list of items. If the Model is null, then the output will be the message "No items found".

Explanation

Razor Control Structures are used to add conditional logic and loops in the Razor view pages. In the above example, the if statement checks if the Model is not null. If the Model is not null, then the list of items is displayed. If the Model is null, then the message "No items found" is displayed.

The foreach loop is used to loop through the items in the Model and create a list of items. This loop is executed only if the Model is not null.

Use

Razor Control Structures are used to create dynamic content in the Razor view pages. You can use these structures to add conditional logic and loops in the view pages. You can perform the check for null values before rendering the view or apply some logic while rendering the view.

Important Points

  • Razor Control Structures can be used to apply conditional logic and loops in the Razor view pages.
  • Control Structures are used to create dynamic content when the page is rendered.
  • Razor Control Structures can be added in the Razor view by using the @ symbol followed by the Razor Code Block that contains the control structures.

Summary

Razor Control Structures are used to add conditional logic and loops in the Razor view pages. These structures are used to create dynamic content when the page is rendered. You can use these structures to add functionalities or perform the check for null values before rendering the view. Razor Control Structures can be used to create dynamic content in the Razor view pages.

Published on: