PartialViewResult Return Type In MVC

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.
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.
  • Its 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 its 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 
and click on OK
HomeControlle.cs
public class HomeController : Controller
    {
        // GET: for main view
        public ActionResult Index()
        {
        return View();
        }
        [HttpGet]
        public PartialViewResult EmpList()
        {
        //for partial view

        return PartialView();
        }
    }
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

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
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 be 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
<div>

   @Html.Partial("EmpList")
</div>
Now run the application the output will be look like as follows

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.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode