liveBook · Manning

View

As explained in ASP.NET Core in Action, 3e

A view is an MVC presentation component that transforms supplied application data into rendered user-interface output such as HTML.

A view is the presentation part of the model–view–controller (MVC) pattern. It takes supplied data and generates the user interface—HTML in a web application. The application model handles business logic; the controller or Razor Page handler handles the request and supplies data for display. Keeping those jobs separate makes UI changes less likely to disturb business logic. section 13.3 section 13.4.4

In ASP.NET Core, a Razor view is a .cshtml template. It can use a view model, partial views, and layouts. Unlike a Razor Page file, an MVC view file must not include an @page directive. A Razor Page has its view as part of the page; an MVC controller selects a separate view to render. section 19.4

An MVC controller action can select a view in two common ways:

  • Return View() to let naming conventions find a template. For HomeController.Index, the default location is Views/Home/Index.cshtml. section 19.4
  • Pass a view model to View(model), or supply a template name with View("ViewToDo", model). The helper creates a ViewResult; views in the Shared folder can also be used by controllers. section 19.4 section 19.6

Displaying a to-do list

For a request to display a to-do category, the handler obtains the needed data from the application model and supplies it for display. The view turns that data into HTML. It does not take over the business logic or decide how to handle the incoming request. section 13.3 section 13.4.4

Listing 19.5 A Razor view
@model ErrorViewModel                                                #1
@{
    ViewData["Title"] = "Error";                                     #2
}
 
<h1 class="text-danger">Error.</h1>                                  #3
<h2 class="text-danger">An error occurred while 
    processing your request.</h2>
 
@if (Model.ShowRequestId)                                            #4
{
    <p>
        <strong>Request ID:</strong> <code>@Model.RequestId</code>   #5
    </p>
}

This Razor view declares a view-model type, writes HTML, and conditionally displays a request ID from the model. section 19.4

Listing 19.7 Returning ViewResult from an action method using default conventions
public class ToDoController : Controller
{
    public IActionResult Index()
    {
        var listViewModel = new ToDoListModel();   #1
        return View(listViewModel);                #2
    }
    public IActionResult View(int id)
    {
        var viewModel = new ViewToDoModel();
        return View("ViewToDo", viewModel);        #3
    }
 
}

These controller actions show the difference between passing a view model with the default template name and passing both a template name and a view model. section 19.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines the MVC role, follows a request through rendering, and shows controller view selection without teaching Razor syntax in detail.
Examples
Uses a to-do category request, an error-page Razor view, and controller actions that return views with optional models or names.
What it assumes
Introduces MVC roles through a to-do request but refers to Razor Page syntax when describing MVC view files.
What you can do after
Apply it via ASP.NET Core MVC’s View() helper to render a Razor view with an optional view model or explicit template name.
How it connects
Connects views to MVC controllers, application models, view models, Razor Pages, and ViewResult.

Also explained in

Compare how 4 books explain view →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.