Here is a clean, developer-friendly CSHTML (Razor) Cheat Sheet — perfect for quick reference. This cheat sheet covers syntax, data binding, tag helpers, loops, conditions, forms, partials, layout, and more.

1. Razor Syntax Basics

Inline C#

HTML / XML
@DateTime.Now
@Model.Name

Code Block

HTML / XML
@{
    var title = "Product List";
}
<h2>@title</h2>

2. Model Binding

Strongly Typed View

HTML / XML
@model Product
<h3>@Model.Name</h3>

List Model

HTML / XML
@model IEnumerable<Product>

@foreach (var p in Model)
{
    <p>@p.Name - @p.Price</p>
}

3. Razor Expressions

Condition

HTML / XML
@if (Model.Stock > 0)
{
    <span>In Stock</span>
}
else
{
    <span>Out of Stock</span>
}

Switch

HTML / XML
@switch (Model.Category)
{
    case "Electronics":
        <span>Electronics</span>
        break;
    default:
        <span>Other</span>
        break;
}

4. Loops

HTML / XML
@for (int i = 0; i < products.Count; i++)
{
    <p>@products[i].Name</p>
}

@foreach (var p in Model)
{
    <p>@p.Name</p>
}

@while (condition)
{
    <p>Running…</p>
}

5. Razor Comments

HTML / XML
@* This will not appear in HTML *@

6. HTML Helpers

HTML / XML
@Html.TextBoxFor(m => m.Name)
@Html.LabelFor(m => m.Price)
@Html.DropDownListFor(m => m.CategoryId, Model.Categories)

7. Tag Helpers (Most Important)

Anchor (link)

HTML / XML
<a asp-action="Details" asp-route-id="@item.Id">View</a>

Form Tag Helper

HTML / XML
<form asp-action="Create" method="post">

Input Tag Helper

HTML / XML
<input asp-for="Name" class="form-control" />

Validation Tag Helpers

HTML / XML
<span asp-validation-for="Name" class="text-danger"></span>
<div asp-validation-summary="All"></div>

Dropdown

HTML / XML
<select asp-for="CategoryId" asp-items="Model.CategoryList"></select>

8. Layout (Master Page Equivalent)

Use Layout

HTML / XML
@{
    Layout = "_Layout";
}

Render Body

In _Layout.cshtml:

HTML / XML
<body>
    @RenderBody()
</body>

Optional Section

HTML / XML
@section Scripts {
    <script src="app.js"></script>
}

9. Partial Views

Render Partial View

HTML / XML
@await Html.PartialAsync("_ProductCard", item)

Render Component (Advanced)

HTML / XML
@await Component.InvokeAsync("CartSummary")

10. TempData, ViewData, ViewBag

ViewData

HTML / XML
@ViewData["Title"]

ViewBag

HTML / XML
@ViewBag.Message

TempData

HTML / XML
@TempData["Success"]

11. Anti-forgery Token

HTML / XML
<form method="post">
    @Html.AntiForgeryToken()

ASP.NET Core automatically generates in <form asp-action>.

12. Using C# Classes in Razor

HTML / XML
@using SampleMVCCoreProject.Models
@model Product

13. Render Scripts and Styles

HTML / XML
@section Styles {
    <link rel="stylesheet" href="~/css/products.css" />
}

@section Scripts {
    <script src="~/js/products.js"></script>
}

14. Razor Shortcuts

SyntaxMeaning
@:Write plain text
@()Inline C# as expression
@{}Code block
@* *@Razor comment

Example:

HTML / XML
@:This is plain text

15. Useful Directives

HTML / XML
@model Product
@using MyProject.Models
@inject IProductService ProductService
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

16. AJAX in CSHTML (No UpdatePanel)

JavaScript
$.get("/Product/GetStock/5", function (data) {
    $("#stockDiv").html(data);
});

17. Render JSON in Razor

HTML / XML
<script>
    var product = @Html.Raw(Json.Serialize(Model));
</script>

18. Raw HTML Rendering

HTML / XML
@Html.Raw(Model.Description)

19. Using JavaScript with Razor Variables

HTML / XML
<script>
    let price = "@Model.Price";
</script>

20. Bootstrap-friendly Razor Table Example

HTML / XML
<table class="table table-striped">
@foreach (var item in Model)
{
    <tr>
        <td>@item.Name</td>
        <td>@item.Category</td>
        <td>@item.Price</td>
        <td>
            <a asp-action="Details" asp-route-id="@item.Id" class="btn btn-primary btn-sm">View</a>
        </td>
    </tr>
}
</table>