← All lessons

Level 1 / ASP.NET Core Razor / lesson 1 of 6

Your first Razor page

Create a page model in C# and render one of its properties in Razor markup.

A Razor page has two halves. The C# half is a plain class holding the data the page needs. The Razor half is markup that reads that data through `Model`. Nothing else happens until you connect them with an `@model` directive.

Understand the idea

Page model versus view

In Razor Pages, the .cshtml.cs page model prepares data and handles requests; the matching .cshtml file renders HTML. A model property must be accessible to the view before @Model.Title can read it.

What @model does

The directive declares the type of Model for the page; it does not create an instance or populate its properties. ASP.NET Core supplies the page model when it handles the request.

Why the boundary matters

Keep request logic in the page model and presentation in the view. A page that works with one hard-coded title still needs a real data source and request handler when it grows.

6 lines
4 lines

Preview - simulated. Not a .NET compiler.

Replace me

Step 1 of 3

In progress

Give the model something to hold

In the C# pane, add a public string property called Title to IndexModel, starting out as "Goose Field Notes".

  • IndexModel declares a public string property named Title - not done yet
  • Title starts out as "Goose Field Notes" - not done yet

Coach

The checklist above is what decides whether a step is done. The coach only explains; it never grades.

The preview is a simulated Razor renderer, not a compiler. It supports @model, @{ } literal assignments, @Expression, @foreach, @if/@else, @* comments *@, and plain markup with class, id, and title attributes. Anything else is reported as outside the lesson instead of being guessed at.

Where the simulator stops

A real Razor Page usually uses a namespace-qualified @model type and inherits from PageModel. This simulated IndexModel is deliberately smaller: it does not run requests or the Razor compiler.

Try it in a real .NET project

In a new ASP.NET Core Razor Pages project, open Pages/Index.cshtml and Pages/Index.cshtml.cs. Add a Title property to the IndexModel, assign it in OnGet, and display it with @Model.Title. Refresh the browser after changing the title.

Check your understanding

If you put @Model.Title in the view but forget @model, which connection is missing?

Show the explanation

The view has no declared page-model type. @model tells Razor which type Model represents; the page model must also expose the Title property and be populated when the request runs.

Goose School teaches Microsoft's stack. Goose Solutions is not affiliated with, endorsed by, or sponsored by Microsoft.