PartialViewResult Type In MVC 5

Background

MVC controller returns many types of output to the view according to the data we need for the application. In this article we will learn about PartialView result type of MVC. So instead of going into the depth on the subject, let us start with its practical implementation.
To know more about the Action result types please refer my previous article,
What is ActionResult

It is the type of output format which is shown to the client.

What is PartialViewResult

PartialViewResult is used to return the partial view. Basically, it is a class which implements the ViewResultBase abstract class that used to render partial view.

PartialViewResult class inherit from ViewResultBase class. So it implements the properties and methods of ViewResultBase class which are used to work and render partial views. The following are the properties of PartialViewResult which are inherited from ViewResultBase.
 
Here are the key points,
  • It does not uses layout page since it render in to the main view which has already layout page.
  • It used to return the portion of web page instead of whole page.
  • It does not fire all page events like main view.
  • It is called into the main view to show list of records and other data to the user.
Properties of PartialViewResult (ViewResultBase)
  • Model
  • TempData
  • View
  • ViewBag
  • ViewData
  • ViewEngineCollection
  • ViewName
Methods of PartialViewResult (ViewResultBase)
  • ExecuteResult
  • FindView
  • Equals()
  • Finalize()
  • GetHashCode()
  • GetType()
  • MemberwiseClone()
  • ToString()
I hope you have understand about the PartialViewResult type from preceding brief summary. Now let's implement it practically.

Step 1 : Create an MVC application.
  1. "Start", then "All Programs" and select "Microsoft Visual Studio 2015".

  2. "File", then "New" and click "Project" then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click on OK.

  3. Choose MVC empty application option and click on OK
Step 3 : Add controller class.

Right click on Controller folder in the created MVC application, give the class name Home or as you wish and click OK.

HomeControlle.cs
  1. public class HomeController : Controller    
  2. {    
  3.     // GET: for main view    
  4.     public ActionResult Index()    
  5.     {    
  6.     return View();    
  7.     }    
  8.     [HttpGet]    
  9.     public PartialViewResult EmpList()    
  10.     {    
  11.     //for partial view    
  12.   
  13.     return PartialView();    
  14.     }    
  15. }   
In the above controller class we have added to action method that is Index which returns the main view and EmpList which returns the partial view.

Step 2 : Add main view

Right click on Home folder inside the View folder in the created MVC application as in the following:



Give the name Index and click Add button.

Step 3
: Add partial view

Right click on Home folder inside the View folder in the created MVC application as in the following:



Step 4
: Run the application and call partial view by typing Home/EmpList with base address. It renders the page as:


In the preceding example of partialview result, you might noticed that it has no layout page.

Step 5: Call Partial view in Main view

Write the following code into main view index to render the partial view.
  1. <div>  
  2.   
  3.    @Html.Partial("EmpList")  
  4. </div> 
Now run the application the output will look like the following screenshot:

 
 Note:
  • Since this is a demo, it might not be using proper standards, so improve it depending on your skills.
Summary

I hope this article is useful for all the readers. If you have any suggestion please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all