Back to: ASP.NET MVC Tutorial For Beginners and Professionals
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. Please read our previous article where we discussed the basics and categories of Action Result in detail. 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 the view page for the given request.
Create 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.
After clicking on the “Project“ link a new dialog will pop up. In that we are going to select web templates from the left pane after selecting the web template, select “ASP.NET Web Application” as shown in the below image.
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.
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 time to create a project for us.
View Result
View result returns basic results to the 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.
View Result is a class and is derived from the “ViewResultBase” class. The “ViewResultBase” is derived from Action Result. View Result base class is an Action Result. Action Result is a base class of different action results.
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 the 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.
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: The Partial View Result is returning the result to a Partial view page. Partial view is one of the views that we can call inside the Normal view page. First, let’s add a Partial View inside the Shared Folder.
Right-Click on the Shared Folder which is inside Views folder and selects Add => View as shown below
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 shared folder as shown below.
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
Note: We need to create a Partial view inside the shared folder although it is not mandatory.
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 am going to discuss FileResult ContentResult EmptyResult JsonResult JavaScript Result in MVC application. 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 needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.
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
Yes ANIL, you are right. It is not mandatory but it is a good programming practice to place partial views within the shared folder.