C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET Razor Control StructuresControl 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. Else and Else IfThe @ (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: @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: @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.
Next TopicASP.Net Razor HTML Helpers
|