How to Create a Partial View in MVC

Partial Views in MVC

This article explains partial views in MVC.

This articles discusses the following topics.

  1. What a partial view is.
  2. Demo showing how to create and use a partial view.
  3. Difference between Html.Partial and Html.RederPartial.
  4. Advantages of using a partial view.

Partial Views are similar to user controls. It is used to create reusable view logic and helps in removing the complexity of views.

The partial views can be used on multiple views where we need similar view logic.

Let's look at an example of it.

In SQL Server I have the following Student database table.

Query used

  1. CREATE DATABASE db_Students;  
  2. Go Use db_Students;  
  3. Go CREATE TABLE tblStudentDetails  
  4. (  
  6. Name NVARCHAR(60),  
  7. Gender NVARCHAR(10),  
  8. Subject NVARCHAR(20)  
  9. );  
  10. INSERT INTO tblStudentDetails  
  11. VALUES  
  12. ('Franklin''Male''C-Sharp'),  
  13. ('Michael''Male''MVC'),  
  14. ('Trevor''Male''Java'),  
  15. ('Lara''Female''PHP'),  
  16. ('Sam''Male''Ruby on Rails'),  
  17. ('Max''Male''C++')  
Let's say we want the preceding table records to be displayed in an MVC application and in addition to that we want to display those records in several views and for that we can copy and paste the code to each and every required view or we can simply create a partial view and using Html.Partial or Html.RenderPartial we can map the partial view in the required views.

The first step is to create a new empty MVC web application.

The second step is to add an ADO.Net entity model and click Add.

Choose "EF Designer from database" and click Next.

Click on "New Connection...".

Specify the server name, credentials and select the database.

Specify the Web.Config name and click Next.

Select the table and click Finish.

The third step is to rename the entity name from tblStudentDetails to Students.

Build the solution.

The fourth step is to add a controller in the Controller directory. So, right-click on the controllers folder and select add controller.

Choose MVC 5 Controller – Empty scaffold and click Add.

Provide the controller the name HomeController and click Add.

At the moment, the HomeController class is present in the PartialViewsInMVC.Controller whereas our model is present in PartialViewsInMVC.Models and in order to use the model we need to import the namespace into our HomeController class.

The fifth step is to create an instance of StudentDbContent inside the Index action method.

  1. StudentDbContext db = new StudentDbContext();  
  2. This object contains a property“Students”which will give us all the students present in Students class.  
  3. public ActionResult Index()   
  4. {  
  5.     StudentDbContext db = new StudentDbContext();  
  6.     List < Students > StudentList = db.Students.ToList();  
  7.     return View(StudentList);  
  8. }

The next step is to add a view. So right-click on the Index action method and select add view.

Provide the View the name Index.

Choose list as a template.

Choose model as Students.

Choose the data context class as StudentDbContext.

Note: At the moment we are not creating a partial view.

Click add

  1. @model IEnumerable  
  2. <PartialViewsInMVC.Models.Students>  
  3. @{  
  4. ViewBag.Title = "Index";  
  5. }  
  6.     <h2>Index</h2>  
  7.     <table class="table">  
  8.         <tr>  
  9.             <th>  
  10. @Html.DisplayNameFor(model => model.Name)  
  11. </th>  
  12.             <th>  
  13. @Html.DisplayNameFor(model => model.Gender)  
  14. </th>  
  15.             <th>  
  16. @Html.DisplayNameFor(model => model.Subject)  
  17. </th>  
  18.         </tr>  
  19. //since the model here is of type IEnumerable, so we need to loop thru each item present in the Model.  
  20. @foreach (var item in Model) {  
  21.         <tr>  
  22.             <td>  
  23. @Html.DisplayFor(modelItem => item.Name)  
  24. </td>  
  25.             <td>  
  26. @Html.DisplayFor(modelItem => item.Gender)  
  27. </td>  
  28.             <td>  
  29. @Html.DisplayFor(modelItem => item.Subject)  
  30. </td>  
  31.         </tr>  
  32. }  
  33.     </table>  

Run the application

Now let's see how to do the same thing using a partial view.

Navigate to the Solution Explorer and expand the Views folder. Inside this folder there is a folder “Shared”.

Add a partial view to this folder.

Choose Create as partial view option.

Click Add

Write the following in the _Students View.

  1. @model PartialViewsInMVC.Models.Students  
  2. <tr>  
  3.     <td>  
  4. @Html.DisplayFor(modelItem => Model.Name)  
  5. </td>  
  6.     <td>  
  7. @Html.DisplayFor(modelItem => Model.Gender)  
  8. </td>  
  9.     <td>  
  10. @Html.DisplayFor(modelItem => Model.Subject)  
  11. </td>  
  12. </tr>  
Write the following in the Index View.
  1. @model IEnumerable  
  2. <PartialViewsInMVC.Models.Students>  
  3.     <table class="table">  
  4.         <tr>  
  5.             <th>  
  6. @Html.DisplayNameFor(model => model.Name)  
  7. </th>  
  8.             <th>  
  9. @Html.DisplayNameFor(model => model.Gender)  
  10. </th>  
  11.             <th>  
  12. @Html.DisplayNameFor(model => model.Subject)  
  13. </th>  
  14.         </tr>  
  15. @foreach (var item in Model)   
  16. {  
  17. @Html.Partial("_Students", item)//the first parameter expects a partial view and the second parameter expects a model object  
  18. }  
  19.     </table>

Note: To render a partial view we use Html.Partial or Html.RenderPartial html helper.

Run the application

Difference between Html.Partial and Html.RenderPartial

The only difference between Html.Partial and Html.RenderPartial is the return type of Html.Partial is MvcHtmlString whereas Html.RenderPartial returns void.

So, if you want to manipulate the data before rendering the view, use Html.Partial else if you want to render directly to the output use Html.RenderPartial since it would be better from a performance perspective.

Syntax for Html.RenderPartial

  1. {Html.RenderPartial("_Students",item); }  
Advantage of using Partial View

Using partial view helps in simplifying the complexity of views. These partial views are reusable and if there is a need to make any change in the code then all we need to do is update the partial view.


In this article we have learned how to create a partial view and we have also seen the two ways by which we can render partial views.

I hope you like it. Thank you.

Similar Articles