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 ASP.NET MVC application. The ASP.NET MVC has 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. Action Results return the result to view page for the given request. 

Please read our last article before proceeding to this article. in the last article, we discussed the basics and categories of Action Result in details.

Create New Asp.Net MVC Application

Open Visual Studio and create a new project

Select File => New => Project

View Result and Partial View Result in ASP.NET MVC

After clicking on “Project” link a new dialog will pop up.

In that we are going to select web templates from the left pane after selecting web template, select “ASP.NET Web Application“.

View Result and Partial View Result in ASP.NET MVC

After selecting this project template next we are going to name the project as “ActionResultsinMVC” and clicking on the OK button a new dialog will pop up with Name “New ASP.NET Project” for selecting project Templates.

View Result and Partial View Result in ASP.NET MVC

In this dialog, we are going to choose MVC project template and then we are going to choose Authentication type for doing that just click on Change Authentication button, a new dialog will pop up with the name “Change Authentication” here we are going to choose No Authentication click on OK Button.

It will take some to time create the project for us. Once the project is created let’s see the folder structure as shown below

View Result and Partial View Result in ASP.NET MVC

View Result

View result is a basic view result. It returns basic results to view page. View result can return data to view page through which class is defined in the model. View page is a simple HTML page. Here view page has “.cshtml” extension.

View Result is a class and is derived from “ViewResultBase” class. “ViewResultBase” is derived from Action Result. View Result base class is an Action Result. Action Result is a base class of different action result.

View Result and Partial View Result in ASP.NET MVC

View Result and Partial View Result in ASP.NET MVC

View Result class is inherited from the Action Result class via View Result Base class. The diagram shown above 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 MVC follows convention-over-configuration approach, MVC will look for a View named “Index” in the Views/Home subfolder, and then look in Views/Shared 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 ASP.NET MVC

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

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

Now it will attempt to find a view named “About” in the Views/Home folder.

Partial View Result 

We can also specify an action to return a partial view instead of a regular view:

Partial View Result is returning the result to Partial view page. Partial view is one of the views that we can call inside Normal view page.

First, let’s add a Partial View inside the Shared Folder.

View Result and Partial View Result in ASP.NET MVC

Right Click on the Shared Folder which is inside Views folder and selects Add => View as shown below

View Result and Partial View Result in ASP.NET MVC

Note: Provide the View name as “_PartialView”, Select 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 shared folder as shown below.

View Result and Partial View Result in ASP.NET MVC

Copy and paste the below code in “_PartialView.cshtml” view

<h3>Its a Partial View</h3>

Let’s modify the index action method to use partial view as shown below.

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

Note: We should create a Partial view inside the shared folder; otherwise we cannot access the Partial view.

Now it is displaying the content of that partial view but 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.

In the next article, I will discuss FileResult ContentResult EmptyResult JsonResult JavaScript Result in MVC application.

SUMMARY

In this article, I try to explain View Result and Partial View Result in ASP.NET MVC application step by step with a simple example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article. 

Leave a Reply

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