TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.Net Razor Control Structures

ASP.Net Razor Control Structures with asp.net tutorial, asp.net introduction, features, project, example, server controls, labels, textbox, button, hyperlink, radiobutton, calender, checkbox, fileupload, events handling, authentication, webforms model binding, html server control, compare validdator, range validator, validation summary, mvc introduction, mvc project, view, validation, entity framework, authentication etc.

<< Back to ASP

ASP.NET Razor Control Structures

Control structures are control statements that are used to control program flow. C# programming language uses if, else, if else, switch, for, foreach, while to perform conditional logic in the application.

Razor engine supports all these controls in the view file. Let's see some examples that implements control structure using razor syntax.


@if

// RazorControlStructure.cshtml

@{
    ViewBag.Title = "RazorControlStructure";
    var value = 20;
}
<hr />
@if (value > 100)
{
    <p>This value is greater than 100.</p>
}
else
{ <p>This value is less than 100.</p>
}

Output:

It produces the following output.

ASP Razor control structure 1

Else and Else If

The @ (at) symbol is not require in else and else if statements.

// RazorControlStructure.cshtml

@{
    Layout = null;
    ViewBag.Title = "RazorControlStructure";
    var value = 5;
}
@if (value > 5)
{
    <p>This value is greater than 5</p>
}
else if (value == 5)
{
    <p>This value is 5.</p>
}
else
{
    <p>This value is less than 5.</p>
}

Output:

ASP Razor control structure 2

@switch Example

// RazorControlStructure.cshtml

@{
    ViewBag.Title = "RazorControlStructure";
    var value = 20;
}
<hr />
@switch (value)
{
    case 1:
        <p>You Entered 1</p>
        break;
    case 25:
        <p>You Entered 25</p>
        break;
    default:
        <p>You entered something than 1 and 25.</p>
        break;
}

Output:

ASP Razor control structure 3

@for

// RazorControlStructure.cshtml

@{
    ViewBag.Title = "RazorControlStructure";
    var value = 5;
}
<hr />
<p>This loop iterates 5 times.</p>
@for (var i = 0; i < value; i++)
{
        <text>@i</text> <br/>
}

Output:

It produces the following output.

ASP Razor control structure 4




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf