Why Partial View In ASP.NET MVC

Partial Views are a type of view which can be reused across the ASP.NET MVC application. It is same as User Control in ASP.NET which can reuse multiple times inside the different views. It is not bound to any particular action method.

When to create Partial View

When you think that the particular data is required again and again in different places it can be single view or different view. In that scenario, we need to create the Partial View. The main purpose of creating the Partial View, writing the code again and again for same output or same piece of information we can create a Partial View and place the data inside it. This can be used anywhere inside the View.

So, basically Partial View is used to reduce the duplication of the same type of data. We can know it as reusable view.

There are different places where we can use Partial View like website header and footer, menu bar, category list, comment section, etc.

comment section

How to create Partial View

Partial View exists inside the \Views\Shared folder. Because it is accessible to all controller. To add new Partial View, Right Click on Shared folder and choose Add and then choose View;

add view

It will open a Add View window, where you can define the name of the Partial View and check the checkbox “Create as a Partial View” for making the view as partial view.

Create as a Partial View

But in ASP.NET MVC 5, you can create different type of view directly as in the following image,

create different type of view

See the following code, which is used for menu bar. If I am going to create multiple view with menu bar then I need to write it multiple times but I can use Partial View here to make my view clear and understandable.

Menu bar HTML code

  1. <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">  
  2.     <div class="container">  
  3.         <div class="navbar-header">  
  4.             <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>  
  5.             <div class="navbar-brand">  
  6.                 <div class="logoimage">  
  7.                     <a href="@Url.Action(" Index ", "Home ")"> <img src="~/Content/Images/logo.png" width="30" height="30" alt="D" /> </a>  
  8.                 </div>  
  9.                 <div class="logotitle"> <a href="@Url.Action(" Index ", "Home ")"><span style="padding-top: 0px; color: white;">Dotnet Tutorial</span></a> </div>  
  10.             </div>  
  11.         </div>  
  12.         <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">  
  13.             <ul class="nav navbar-nav navbar-right">  
  14.                 <li class="active"> <a href="@Url.Action(" Index ", "Home ")">Home</a> </li>  
  15.                 <li> <a href="@Url.Action(" Index ", "Tutorials ")">Tutorials</a> </li>  
  16.                 <li> <a href="@Url.Action(" Index ", "Articles ")">Articles</a> </li>  
  17.                 <li> <a href="@Url.Action(" Index ", "interview ")">Interviews</a> </li>  
  18.                 <li> <a href="@Url.Action(" AboutUs ", "Home ")">About</a> </li>  
  19.                 <li> <a href="@Url.Action(" contactus ", "Home ")">Contact</a> </li>  
  20.             </ul> } </li>  
  21.             </ul>  
  22.         </div>  
  23.     </div>  
  24. </nav>  
To manage it, we can create a _headerPartialView.cshtml partial view inside the Shared folder and place this code inside it and reuse it anywhere we ant as in then following,
  1. @Html.Partial("_headerPartialView ")  
Partial View inside Partial View

We can also use a Partial View inside a Partial View. See the following example, here I created a SideBar.cshtml Partial View, which will show all the shortcut menu items on the website. But to show the items I have again created more partial views.

Index.cshtml
  1. @Html.Partial("SideBar")  
SideBar.cshtml
  1. <div>  
  2.     <div class="panel panel-default text-left">  
  3.         <div class="panelheaderOrange"> Search </div>  
  4.         <div class="panel-body">  
  5.             <div class="input-group"> @Html.Partial("SearchBar") </div>  
  6.         </div>  
  7.     </div>  
  8. </div>  
  9. <div>  
  10.     <div class="panel panel-default text-left">  
  11.         <div class="panelheaderblue"> Categories </div>  
  12.         <div class="panel-body">  
  13.             <div id="dvHomeCategoriesBind">  
  14.                 <div class="loaderCenter"> @*<img src="~/Content/img/loader.gif" />*@<span class="loadingText">Category Loading....</span> </div>  
  15.             </div>  
  16.         </div>  
  17.     </div>  
  18. </div>  
  19. <div>  
  20.     <div class="panel panel-default text-left">  
  21.         <div class="panelheaderRed"> Recent Articles </div>  
  22.         <div class="panel-body">  
  23.             <div id="dvHomeRecentPostBind">  
  24.                 <div class="loaderCenter"> <span class="loadingText">Recent Articles Loading....</span> </div>  
  25.             </div>  
  26.         </div>  
  27.     </div>  
  28. </div>  
  29. <div>  
  30.     <div class="panel panel-default text-left">  
  31.         <div class="panelheaderGreen"> Popular Articles </div>  
  32.         <div class="panel-body">  
  33.             <div id="dvHomePopularPostBind">  
  34.                 <div class="loaderCenter"> <span class="loadingText">Popular Articles Loading....</span> </div>  
  35.             </div>  
  36.         </div>  
  37.     </div>  
  38. </div> @Html.Partial("FollowUs") @Html.Partial("FindUsOnFacebook") @Html.Partial("FindUsOfGoogle")  
See above, we are using the Partial View with different ways. Actually Partial View is nothing but only a DIV. So, we can bind it directly from jQuery.

MVC View

How to bind Partial View with Data

There are various ways to bind the data of Partial View, but according to me, you should always prefer to bind the data using JQuery or Ajax calling. See the following code how I am going to bind the SideBar.cshtml Partial View data.
  1. function LoadHomeCategory()  
  2. {  
  3.     $.ajax(  
  4.     {  
  5.         url: "/Categories/BlogCategories",  
  6.         contentType: "application/html; charset=utf-8",  
  7.         type: "GET",  
  8.         cache: !0,  
  9.         datatype: "html",  
  10.         success: function (t)  
  11.         {  
  12.             $("#dvHomeCategoriesBind")  
  13.                 .html(t)  
  14.         },  
  15.         error: function ()  
  16.         {  
  17.             $("#dvHomeCategoriesBind")  
  18.                 .html("Category Not Found")  
  19.         }  
  20.     })  
  21. }  
  22.   
  23. function LoadHomeRecentPost()  
  24. {  
  25.     $.ajax(  
  26.     {  
  27.         url: "/Recents/RecentPosts",  
  28.         contentType: "application/html; charset=utf-8",  
  29.         type: "GET",  
  30.         cache: !0,  
  31.         datatype: "html",  
  32.         success: function (t)  
  33.         {  
  34.             $("#dvHomeRecentPostBind")  
  35.                 .html(t)  
  36.         },  
  37.         error: function ()  
  38.         {  
  39.             $("#dvHomeRecentPostBind")  
  40.                 .html("Post Not Found")  
  41.         }  
  42.     })  
  43. }  
  44.   
  45. function LoadHomePopularPost()  
  46. {  
  47.     $.ajax(  
  48.     {  
  49.         url: "/Recents/PopularPosts",  
  50.         contentType: "application/html; charset=utf-8",  
  51.         type: "GET",  
  52.         cache: !0,  
  53.         datatype: "html",  
  54.         success: function (t)  
  55.         {  
  56.             $("#dvHomePopularPostBind")  
  57.                 .html(t)  
  58.         },  
  59.         error: function ()  
  60.         {  
  61.             $("#dvHomePopularPostBind")  
  62.                 .html("Post Not Found")  
  63.         }  
  64.     })  
  65. }  
  66.   
  67. function LoadRelatedPost()  
  68. {  
  69.     $.ajax(  
  70.     {  
  71.         url: "/Recents/RelatedPosts",  
  72.         contentType: "application/html; charset=utf-8",  
  73.         type: "GET",  
  74.         cache: !0,  
  75.         datatype: "html",  
  76.         success: function (t)  
  77.         {  
  78.             $("#dvRelatedPostBind")  
  79.                 .html(t)  
  80.         },  
  81.         error: function ()  
  82.         {  
  83.             $("#dvRelatedPostBind")  
  84.                 .html("Post Not Found")  
  85.         }  
  86.     })  
  87. }  
  88. $(document)  
  89.     .ready(function ()  
  90.     {  
  91.         LoadHomeCategory(), LoadHomeRecentPost(), LoadHomePopularPost()  
  92.     });  
Thanks for reading the article, hope you enjoyed it.

 


Similar Articles