View Result in ASP.NET Core MVC

View Result in ASP.NET Core MVC

In this article, I will discuss the View Result in the ASP.NET Core MVC Applications with Examples. Please read our previous article, where we discussed the basic concepts of Action Results in ASP.NET Core MVC Application.

ViewResult in ASP.NET Core MVC

In ASP.NET Core MVC, View Result refers to one of the possible types of action results returned by the controller action methods. In the controller action method, the return type indicates what should be rendered or returned to the client (typically a web browser).

A View Result specifically instructs ASP.NET Core MVC to render a view template as the response to the client’s request. A view is a file with a .cshtml extension that contains the HTML markup with C# code using Razor syntax. This allows for dynamic HTML generation based on the data passed from the controller.

The ViewResult class derives from the ActionResult base class. If you go to the definition of ViewResult class, then you will see the following signature. As you can see, it is a concrete class with a few properties, overriding the ExecuteResultAsync method, which is used to generate the dynamic HTML response.

ViewResult in ASP.NET Core MVC

Understanding ExecuteResultAsync Method of ViewResult class:

In ASP.NET Core MVC, the ExecuteResultAsync method of the ViewResult class is responsible for rendering a view to generate the HTML output that will be sent to the client’s browser. This method is invoked when the action result returned by a controller action is a ViewResult. The following things are going to happen when the ExecuteResultAsync method is called:

  • View Discovery: It searches for a view file based on the specified view name, which defaults to the action name if not explicitly set.
  • View Rendering: Once the view is located, the method renders it to generate the HTML content. During rendering, the view engine processes any markup, server-side code (if present), and data passed to the view.
  • Response Generation: After rendering the view, the generated HTML content is written to the response stream. This content will be sent to the client’s browser as the HTTP response body.
  • Response Finalization: Finally, any additional HTTP headers and status codes are set as necessary, and the response is finalized and sent to the client.
How to Use ViewResult in ASP.NET Core MVC?

To use ViewResult in ASP.NET Core MVC Application, we need to follow the below steps:

  1. Define a Razor View: Start by creating a Razor view (with the .cshtml extension) in your project’s appropriate “Views” folder. This view will contain the HTML markup and any embedded Razor code (C# Code) needed to render dynamic content.
  2. Create an Action Method: Create an action method within your controller that will return a ViewResult. This method will process data and pass it to the view.
  3. Return a View: In the action method, return a ViewResult by calling the View() method. You can pass the model object to the View() method if you need to supply dynamic data to the view.
Example to Understand ViewResult in ASP.NET Core MVC:

Let us understand how to use ViewResult in ASP.NET Core MVC Application with one example. First, create a new ASP.NET Core MVC Project using the Model View Controller template and name the project ActionResultInASPNETCoreMVC.

Creating Model:

Next, create a model class to hold the product data. So, create a class file named Products.cs within the Models folder and copy and paste the following code into it.

namespace ActionResultInASPNETCoreMVC.Models
{
    public class Product
    {
        public int Id { get; set; }
        public string? Name { get; set; }
    }
}
Modify the Home Controller:

Next, modify the Home Controller as follows. In the code below, the return type of the Index action method is ViewResult. The ViewResult returned by the View() method will render the Index.cshtml Razor view, and we pass the product model object to the Index.cshtml view to populate dynamic content within the view.

using ActionResultInASPNETCoreMVC.Models;
using Microsoft.AspNetCore.Mvc;
namespace ActionResultInASPNETCoreMVC.Controllers
{
    public class HomeController : Controller
    {
        public ViewResult Index()
        {
            Product product = new Product()
            {
                Id = 1,
                Name = "Test",
            };
            return View(product);
        }
    }
}
Modifying the Index View:

Next, modify the Index.cshtml file as follows:

@model Product
@{
    ViewData["Title"] = "Home Page";
    Layout = null;
}

<div class="text-left">
    <p>Product ID: @Model.Id</p>
    <p>Product Name: @Model.Name</p>
</div>

Now, run the application, and you should get the expected output, as shown in the image below.

View Results (View Result and Partial View Result) in the ASP.NET Core MVC application

In the next article, I will discuss the Partial View Result in ASP.NET Core MVC Applications. In this article, I try to explain View Result in ASP.NET Core MVC Application with Examples. I hope you enjoy this ViewResult in ASP.NET Core MVC Application with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *