liveBook · Manning

ViewResult

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

ViewResult is an MVC action-result type that represents rendering a named view in response to an action.

A `ViewResult` tells ASP.NET Core MVC to render a view instead of returning a plain string. A controller action creates one by calling `View`, optionally supplying a view model for the view to use when generating HTML. section 2.3.3 section 2.3.4

Listing 2.7 Rendering a view in the HomeController.cs file in the Controllers folder
using Microsoft.AspNetCore.Mvc;

namespace FirstProject.Controllers {

    public class HomeController : Controller {
        public ViewResult Index() {
            return View("MyView");
        }
    }
}

`HomeController.Index` returns `View("MyView")`, selecting the named view for the response. section 2.3.3

For that action, place `MyView.cshtml` in `Views/Home`. If the file is missing, a request to `http://localhost:5000` produces an error showing where ASP.NET Core looked. Controller-specific views live under `Views/<ControllerName>`; `Views/Shared` holds views not specific to one controller. section 2.3.3

Listing 2.9 Using a view model in the HomeController.cs file in the Controllers folder
using Microsoft.AspNetCore.Mvc;

namespace FirstProject.Controllers {

    public class HomeController : Controller {
 
        public ViewResult Index() {
            int hour = DateTime.Now.Hour;
            string viewModel = 
                hour < 12 ? "Good Morning" : "Good Afternoon";
            return View("MyView", viewModel);
        }
    }
}

The action chooses a greeting from the current hour and passes it as the second argument to `View("MyView", viewModel)`. The corresponding Razor view declares `@model string` and inserts the value with `@Model`. section 2.3.3

Thank a form respondent

After an RSVP form is submitted, `RsvpForm` returns a `ViewResult` selecting `Thanks` and passing the model-bound `GuestResponse`. In `Views/Home/Thanks.cshtml`, `@Model?.Name` addresses the guest, while `WillAttend` determines which message appears. section 3.2.7

The request path is straightforward: routing selects an action; the action returns a `ViewResult` containing a view name and model; the Razor view engine locates and processes the view; the resulting response goes to the browser. section 2.3.4

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

How deep it goes
Shows creation, view lookup, model passing, and request flow; defers detailed Razor syntax.
Examples
Uses a greeting view and an RSVP thank-you view driven by a submitted response.
What it assumes
Assumes familiarity with C# controller actions while demonstrating view creation and basic Razor model access.
What you can do after
Apply it via ASP.NET Core MVC to return named views with models and check view-location errors.
How it connects
Connects controller actions and routing to Razor views, view models, and model binding.

Also explained in

Compare how 2 books explain ViewResult →

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.