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#
@DateTime.Now
@Model.Name
Code Block
@{
var title = "Product List";
}
<h2>@title</h2>
2. Model Binding
Strongly Typed View
@model Product
<h3>@Model.Name</h3>
List Model
@model IEnumerable<Product>
@foreach (var p in Model)
{
<p>@p.Name - @p.Price</p>
}
3. Razor Expressions
Condition
@if (Model.Stock > 0)
{
<span>In Stock</span>
}
else
{
<span>Out of Stock</span>
}
Switch
@switch (Model.Category)
{
case "Electronics":
<span>Electronics</span>
break;
default:
<span>Other</span>
break;
}
4. Loops
@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
@* This will not appear in HTML *@
6. HTML Helpers
@Html.TextBoxFor(m => m.Name)
@Html.LabelFor(m => m.Price)
@Html.DropDownListFor(m => m.CategoryId, Model.Categories)
7. Tag Helpers (Most Important)
Anchor (link)
<a asp-action="Details" asp-route-id="@item.Id">View</a>
Form Tag Helper
<form asp-action="Create" method="post">
Input Tag Helper
<input asp-for="Name" class="form-control" />
Validation Tag Helpers
<span asp-validation-for="Name" class="text-danger"></span>
<div asp-validation-summary="All"></div>
Dropdown
<select asp-for="CategoryId" asp-items="Model.CategoryList"></select>
8. Layout (Master Page Equivalent)
Use Layout
@{
Layout = "_Layout";
}
Render Body
In _Layout.cshtml:
<body>
@RenderBody()
</body>
Optional Section
@section Scripts {
<script src="app.js"></script>
}
9. Partial Views
Render Partial View

Join the conversation! Your thoughts help the community grow.