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

