liveBook · Manning

View

As explained in Pro ASP.NET Core 7, Tenth Edition

A view is a presentation component or template that renders application data into a user-interface response, typically HTML.

A view turns an MVC controller action’s result into an HTML response. A Razor view is a `.cshtml` file containing HTML and C# expressions; the Razor view engine evaluates the expressions and combines their results with the markup. Use a view when the browser needs rendered HTML rather than a plain string or a JSON response. section 2.3.3 section 21.0

To render one from a controller:

  • Create an action that returns `View()`. This selects the default view named after the action: an `Index` action uses `Index.cshtml`. Pass data with `View(model)`, or select a different file with `View("Thanks", model)`. section 3.2.1 section 3.2.7
  • Put a controller-specific file in `Views/<ControllerName>/`; put a view intended for multiple controllers in `Views/Shared/`. If a named view is missing, ASP.NET Core reports the locations it searched. section 2.3.3 section 30.1
  • Declare the expected data type with `@model`, then use `Model` in expressions such as `@Model.Name`. Razor’s `@foreach` can generate HTML for each item in a collection. Model data can be null even when the declared type is not nullable, so account for an empty or missing sequence. section 3.2.8 section 7.3.2

Render a response after an RSVP

A POST action can receive a `GuestResponse` through model binding and return `View("Thanks", guestResponse)`. In `Views/Home/Thanks.cshtml`, `@model PartyInvites.Models.GuestResponse` makes its properties available to the view. `@Model?.Name` personalizes the heading, while `@if (Model?.WillAttend == true)` chooses the attending or declining message. The action handles the submitted data; the view decides how to display the response. section 3.2.7

Listing 7.25 Using the product data in the Index.cshtml file in the SportsStore/Views/Home folder
@model IQueryable<Product>

@foreach (var p in Model ?? Enumerable.Empty<Product>()) {
   <div>
       <h3>@p.Name</h3>
       @p.Description
       <h4>@p.Price.ToString("c")</h4>
   </div>
}

This product-list view shows the boundary in practice: the action supplies products, while the view loops over them and formats their HTML without knowing how they were obtained. section 7.3.2

Razor views are compiled into C# classes derived from `RazorPage<T>`. Generated code writes literal markup separately from expression results; `Write` encodes expression output for safe inclusion in HTML. To inspect generated C#, enable `EmitCompilerGeneratedFiles` in the project file and build; the example output is under `obj/Debug/net7.0/generated`. Views can also use layouts, with `_ViewStart.cshtml` setting a default layout and a layout’s `RenderBody()` inserting the view’s content. section 21.3 section 32.5

For a working MVC setup, register controller-with-view services and map a controller route. Create or edit the `.cshtml` file, run the application, and request the action in a browser. With `dotnet watch`, view edits are recompiled and the browser reloads automatically. Razor Pages reuse view syntax and features, but add a `@page` directive and page-model arrangement; they are not controller-selected views. section 21.0 section 21.2.3 section 23.5

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Builds and selects views, demonstrates typed models and conditional markup, and traces Razor compilation and output encoding.
Examples
Uses party RSVP confirmations and attendee lists, product listings, and generated C# from a Razor view.
What it assumes
Introduces view placement and basic Razor expressions while assuming an ASP.NET Core application with controller actions and model data.
What you can do after
Build it from scratch: create and select Razor views, pass model data, and render dynamic HTML from controller actions.
How it connects
Connects MVC controllers, action results, view models, Razor syntax, layouts, and Razor Pages.

Also explained in

Compare how 2 books explain view →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.