View Result and Partial View Result in MVC

View Result and Partial View Result in ASP.NET MVC

In this article, I am going to discuss the View Result and Partial View Result in the ASP.NET MVC application. Please read our previous article, where we discussed the Action Results in ASP.NET MVC Application. The ASP.NET MVC Framework provides different types of Action Results. Each action result returns a different format of the output. As a programmer, we need to use different action results to get the expected output. 

Create a New ASP.NET MVC Application

Open Visual Studio and create a new project. To do so, select File => New => Project, as shown in the below image.

Creating Project in Visual Studio

A new dialog will pop up after clicking on the “Project” link. In that, we are going to select web templates from the left pane; after selecting the web template, select ASP.NET Web Application, and next, we are going to name the project “ActionResultsinMVC” and click on the OK button as shown in the below image.

Selecting Project Type in Visual Studio

Once you click on the OK button, a new dialog will pop up with the Name “New ASP.NET Project” for selecting project Templates. In this dialog, we will choose the MVC project template, and then we will choose the Authentication type. To do that, click on the Change Authentication button, and a new dialog will pop up with the name “Change Authentication“. Here, we are going to choose No Authentication and click on the OK Button. 

Selecting Project Template in Visual Studio

Once you click on the OK button, creating a project for us will take some time. Let us first understand the need and use of ViewResult, and then we will understand the need and use of PartialViewResult in ASP.NET MVC Applications.

View Result in ASP.NET MVC:

The View Result in MVC returns the result to a View Page. The View Result can return data to the View Page through the model class. The view page is a simple HTML page. Here view page has a “.cshtml” extension. The ViewResult is a class and is derived from the “ViewResultBase” class. The “ViewResultBase” is derived from ActionResult class, as shown in the below image. So, ViewResult is indirectly derived from the ActionResult abstract class. And we already know that ActionResult is the base class of different action results.

View Result Class in ASP.NET MVC

View Result class is inherited from the Action Result class via the View Result Base class. The above diagram shown describes the inheritance of Action Results.

Example: Let’s see the HomeController Index Action method to understand View Result
public class HomeController : Controller
{
    public ViewResult Index()
    {
        return View();
    }
}

Since ASP.NET MVC follows the convention-over-configuration approach, MVC will look for a View named “Index” in the Views/Home subfolder and then look in the Views/Shared subfolder. If it doesn’t find it, then it will throw an InvalidOperationException. So when we navigate to Home/index it will display the following page.

View Result and Partial View Result in MVC

What if I wanted to return a view other than the one that matches the action name? Then we need to specify the View name as shown below explicitly.

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

Now it will attempt to find a view with the name “About” in the Views/Home folder, and if it is not found there, then it will search the Views/Shared subfolder.

Partial View Result in ASP.NET MVC Application

Returning a Partial View instead of a View from an Action Method in the ASP.NET MVC Application is also possible. The Partial View Result in MVC is returning the result to a Partial View Page. A partial view is one of the views that we can call inside a Normal view page. First, let’s add a Partial View inside the Shared Folder.

Partial View in ASP.NET MVC

Right-Click on the Shared Folder, which is inside the Views folder, and then select Add => View option from the context menu, which will open the following Add view window.

Creating Partial View in ASP.NET MVC

Note: Provide the View name as “_PartialView”, select the Template as Empty, and then check the Create as a partial view checkbox and click on Add button, which will add the Partial view in the shared folder as shown in the below image.

Adding Partial View in ASP.NET MVC Application

Now open _PartialView.cshtml file and then Copy and paste the below code into it.

<h3>Its a Partial View</h3>

As shown below, let’s modify the index action method to Return a Partial View.

public class HomeController : Controller
{
    public PartialViewResult Index()
    {
        return PartialView("_PartialView");
    }
}

Run the application and navigate to Home/Index; it will display the page as shown below.

View Result and Partial View Result in ASP.NET MVC

Now it displays the content of that partial view without the layout page. This isn’t very useful by itself, so a more useful application might be to call this action in an AJAX scenario and display the returned view. We will discuss this in a later article.

Note: We need to create a Partial view inside the shared folder, although it is not mandatory. In this article, I show you how to use the Partial View Result in ASP.NET MVC Application. In a later article, we will discuss real-time examples of using Partial Views in the ASP.NET MVC Application.

In the next article, I am going to discuss FileResult ContentResult EmptyResult JsonResult JavaScript Result in the ASP.NET MVC application. In this article, I try to explain View Results and Partial View Results in ASP.NET MVC application with Examples. I hope this View Result and the Partial View Result in the MVC article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this View and Partial View Results in the MVC article. 

4 thoughts on “View Result and Partial View Result in MVC”

  1. Note: We should create a Partial view inside the shared folder; otherwise we cannot access the Partial view.
    hi Sir,
    This Point is wrong.
    i created Partial view in other folder. and access from other folder child view .it working fine

Leave a Reply

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