liveBook · Manning

Model directive

The @model directive is a Razor directive that declares the model type associated with a page or view.

How 2 Manning books explain it

Pro ASP.NET Core 7, Tenth Edition

The Razor model directive declares the .NET type of the view model exposed to a Razor view.

How it is used
Declares the model type a Razor view expects, or selects the page model class in a Razor Page.
How deep it goes
Explains generated `RazorPage<T>`, editor and build-time checking, nullable view data, view imports, and the distinct Razor Page use.
Examples
Uses product-detail and product-list views, a product table-row partial, and a Razor Page that displays a loaded product.
What you can do after
Apply it in CSHTML to type-check model expressions and distinguish controller-view models from Razor Page models.

Read how Pro ASP.NET Core 7, Tenth Edition explains model directive →

ASP.NET Core in Action, 3e

The @model directive is a Razor directive that declares the PageModel type associated with a Razor Page.

How it is used
Declares the PageModel type that a Razor Page uses to handle requests and provide strongly typed data to its view.
How deep it goes
Explains placement, type association, request processing, binding, and handler execution, while deferring PageModel details.
Examples
Uses PrivacyModel and ToDoItemModel declarations, including the conventional .cshtml.cs code-behind pairing.
What you can do after
Apply the directive to associate a Razor Page with a PageModel and access its typed properties during HTML generation.

Read how ASP.NET Core in Action, 3e explains model directive →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.