โœจ ASP.NET Razor Syntax
Estimated reading: 3 minutes 57 views

๐Ÿ”ฃ Razor C# โ€“ Logic โ€“ Control Flow with Conditionals in Razor Views

๐Ÿงฒ Introduction โ€“ What Is Razor Logic in ASP.NET?

Razor C# logic refers to the use of control flow statements such as if, else, switch, and ternary operators within Razor views. These allow developers to render dynamic UI based on conditions, flags, or user state, giving your .cshtml pages intelligent behavior without bloating the code.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to write conditional logic in Razor views
  • How to use if, else, else if, switch, and ternary operations
  • How to mix logic with clean HTML output
  • Best practices for separating view logic from business logic

โœ… Razor if Statement

@{
    bool isLoggedIn = true;
}

@if (isLoggedIn)
{
    <p>Welcome back, user!</p>
}

๐Ÿงช Output:
If isLoggedIn is true โ†’ shows: Welcome back, user!


๐Ÿ” if...else and else if Example

@{
    int score = 85;
}

@if (score >= 90)
{
    <p>Grade: A</p>
}
else if (score >= 80)
{
    <p>Grade: B</p>
}
else
{
    <p>Grade: C or below</p>
}

๐Ÿงช Output:
Grade: B


๐Ÿ”„ Using switch in Razor

@{
    string role = "Admin";
}

@switch (role)
{
    case "Admin":
        <p>You have full access.</p>
        break;
    case "Editor":
        <p>You can edit content.</p>
        break;
    default:
        <p>Read-only access.</p>
        break;
}

๐Ÿงช Output:
You have full access.


โ” Ternary Operator in Razor

@{
    bool isPremium = false;
}

<p>Account Type: @(isPremium ? "Premium" : "Standard")</p>

๐Ÿงช Output:
Account Type: Standard


๐Ÿ“ฆ Logic in HTML Attributes

<input type="text" class="form-control @(hasError ? "is-invalid" : "")" />

๐Ÿ” Adds is-invalid class conditionally for form validation styling.


๐Ÿงฌ Combining Logic with Loops

@{
    var prices = new[] { 100, 200, 300 };
}

<ul>
@foreach (var price in prices)
{
    if (price > 150)
    {
        <li>High: @price</li>
    }
    else
    {
        <li>Low: @price</li>
    }
}
</ul>

๐Ÿงช Output:

  • Low: 100
  • High: 200
  • High: 300

๐Ÿ“˜ Best Practices for Razor Logic

โœ… Do:

  • Use @if, @switch, and @for for clean UI logic
  • Keep business rules in your controller or PageModel
  • Use ternary only for short, readable conditions

โŒ Avoid:

  • Writing deeply nested if statements in Razor
  • Embedding database or calculation logic in .cshtml files
  • Mixing markup and complex logic in a single line

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Razorโ€™s C# logic features let you control UI output dynamically and conditionally. From if blocks to switch statements, Razor simplifies rendering different content based on application state or model data.

๐Ÿ” Key Takeaways:

  • Use @if, @else, and @switch for conditional output
  • Ternary operators are useful for inline HTML attributes
  • Keep complex logic in controller or backend

โš™๏ธ Real-world Use Cases:

  • Show/hide UI based on login status
  • Format prices, badges, or roles conditionally
  • Highlight validation errors dynamically

โ“ FAQs โ€“ Razor Logic in Views


โ“ Can I write full C# logic in Razor?
โœ… Yes, Razor supports full C# syntax like if, for, switch, and foreach inside views.


โ“ Should I do heavy logic in .cshtml?
โœ… No. Keep Razor logic light. Use controllers or view models for complex conditions.


โ“ Is Razor logic secure?
โœ… Razor executes on the server, so users cannot view the logic. Only the final HTML output is sent to the browser.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ”ฃ Razor C# โ€“ Logic

Or Copy Link

CONTENTS
Scroll to Top