ASP.NET Core  

What You Can & Cannot Do in CSHTML (Razor Views)

Below is a well-structured blog post explaining what is possible in CSHTML (Razor Views) and what is NOT possible compared to ASP.NET Web Forms, along with examples, attributes, and key concepts.

ASP.NET Core MVC introduced Razor View Engine (CSHTML files), replacing the old ASP.NET Web Forms (.aspx, .ascx) system.

Many developers coming from Web Forms often get confused:

“Where is GridView? Where is DataBind()? Can I use UpdatePanel? Why can't I drag and drop controls?”

This blog post clears all doubts with examples, comparisons, and CSHTML capabilities.

1. What Is CSHTML in ASP.NET Core?

CSHTML = C# + HTML
It is a Razor template file that allows embedding C# code inside HTML in a clean, lightweight way.

Example

@{
    var message = "Hello Razor!";
}

<h2>@message</h2>

Razor follows the philosophy:

“Generate HTML using C#, but keep UI logic minimal and clean.”

2. CSHTML vs Web Forms — Key Differences

FeatureCSHTML (Razor)Web Forms (ASPX)
Server ControlsNot supportedGridView, Repeater, TextBox, Button
ViewStateNo ViewStateHeavy ViewState
Auto PostbackNot supportedAutoPostback events
Drag-and-drop UINoYes
DataBind()Not availableRequired
Page Life CycleNo page eventsPage_Load, PreInit, OnInit
Data BindingModern Razor bindingServer data controls binding
HTML control freedomFull controlLimited
PerformanceVery fast, lightweightSlow because of ViewState

3. What IS POSSIBLE in CSHTML (Razor)?

3.1 Use loops (for, foreach, while)

Example

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

3.2 Conditionals (if/else, switch)

@if (Model.IsActive)
{
    <span class="badge bg-success">Active</span>
}
else
{
    <span class="badge bg-danger">Inactive</span>
}

3.3 Strongly Typed Models

@model IEnumerable<Product>

You can bind data directly without DataBind().

3.4 Tag Helpers (Modern equivalent of server controls)

Example: Action link button

<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" />

3.5 Partial Views (instead of UserControls)

@await Html.PartialAsync("_ProductCard", product)

3.6 Sections and Layouts

Reusable layout like MasterPage:

@layout "~/Views/Shared/_Layout.cshtml"

3.7 AJAX & jQuery instead of UpdatePanel

Example AJAX call:

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

4. What is NOT Possible in CSHTML (Compared to Web Forms)

4.1 No Server Controls (GridView, Repeater, DropDownList etc.)

You cannot do:

<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="true" />

Instead you must build HTML manually:

<table>
@foreach (var item in Model)
{
    <tr><td>@item.Name</td></tr>
}
</table>

4.2 No ViewState

Meaning:

  • No automatic state management

  • No hidden page lifecycle data

  • Much faster performance

4.3 No AutoPostBack

You cannot do:

AutoPostBack="true"
OnSelectedIndexChanged="ddlChanged"

You must use:

1.JavaScript

2.jQuery

3.AJAX

4.Fetch API

4.4 No Page Life Cycle Events

No such events:

  • Page_Load

  • Page_Init

  • Page_PreRender

Instead ASP.NET Core uses:

1. Controller actions

2. Dependency Injection

3. Middleware

4.5 No UpdatePanel

UpdatePanel was used for partial page refresh.
In MVC Core, you must use:

  • AJAX

  • Fetch API

  • jQuery

  • Partial Views

  • HTMX / SignalR

5. Examples: How CSHTML Replaces Web Forms Controls

Example 1: GridView → Razor Table

Web Forms GridView

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="true">
</asp:GridView>

Razor (CSHTML)

<table class="table table-bordered">
@foreach (var item in Model)
{
    <tr>
        <td>@item.Name</td>
        <td>@item.Price</td>
    </tr>
}
</table>

Example 2: UserControl → Partial View

Web Forms

<uc1:ProductDetail ID="ProductDetail1" runat="server" />

Razor

@await Html.PartialAsync("_ProductDetail", Model)

Example 3: UpdatePanel → AJAX

Web Forms

<asp:UpdatePanel runat="server">
   <ContentTemplate>

Razor + jQuery

$("#loadBtn").click(function () {
    $("#result").load("/Product/Partial");
});

6. Attributes in Razor (Tag Helpers)

AttributePurpose
asp-forBinds to model property
asp-actionCalls controller action
asp-controllerSpecifies controller
asp-route-idPass route parameter
asp-validation-forShows validation errors
asp-itemsBinds dropdown values

Example

<select asp-for="CategoryId" asp-items="Model.Categories"></select>

7. Summary — Quick Comparison

Razor CSHTML Allows

  • HTML + C#

  • Loops, conditions

  • Models

  • Tag Helpers

  • Partial Views

  • AJAX

  • Custom HTML UI

Razor Does NOT Allow:

  • GridView, Repeater, DataList

  • UpdatePanel

  • ViewState

  • Code-behind UI logic

  • Drag-and-drop server controls