Best Way To Bind Partial Views For Improving Performance

There are several ways to bind partial views and display them on views. But what is the best way? When I was working on one of my projects, I was concerned about which techniques I would need to use to make my website faster, and also if we are binding multiple partial views on the same page,  which are getting the data from the database. So, those things are very complicated and in this scenario we need to choose the way which will load every partial view independently.

So, today I will discuss that. Whenever we talk about the partial view data to bind with it, we find several ways to bind the partial view. The following are a few examples.

We can bind some static data with partial view as following

@Html.Partial("SideBar")

We can also direct bind the partial view with Model.

@Html.Partial("RecentPost", Model.RecentPostl)

Partial view can be bind with ViewBag as well.

@Html.Partial("_MyPartial", (double)@ViewBag.piValue)

Now I am going to show different ways to bind the partial views.

First Way [Bad Way, Don’t Use]

I have seen many developers bind all the partial views on a single request. Actually they try to get all the partial views data as entity on action result and return view with model data. As inthe  following example, you can see that I need Recent Post and Popular Post.

  1. public class SideBarPostViewModel  
  2. {  
  3.     public List < RecentPost > RecentPost  
  4.     {  
  5.         get;  
  6.         set;  
  7.     };  
  8.     public List < PopularPost > PopularPost  
  9.     {  
  10.         get;  
  11.         set;  
  12.     };  
  13. }  
But on the controller, I have used only one Action Result for getting the data for both partial views.
  1. public ActionResult GetAllPost()  
  2. {  
  3.     var model = new SideBarPostViewModel();  
  4.     model.RecentPost = _db.getRecentPostList();  
  5.     model.PopularPost = _db.getPopularPostList();  
  6.   
  7.     return View(model);  
  8. }  
You can see clearly i the following code that on the single view I am passing the multiple partial view which data is coming from a single request.
  1. @model AspNetDemo.ViewModels.SideBarPostViewModel  
  2.   
  3. <b>  
Here you can pass some other view data,
  1. </b>  
  2.   
  3. @Html.RenderPartial("RecentPostPartialView", model.RecentPost);  
  4.   
  5. @Html.RenderPartial("PopularPostPartialView", model.PopularPost);  
So, as per concern of performance, this is a bad way to bind the partial view.

Second Way [So-So Good]

We can also return the partial view fromthe  action result. So, basically in this scenario we return a partial view as a div.
  1. @ { Html.RenderAction("GetMyAddress""User");  
  2. }  
  3. [ChildActionOnly]  
  4. public PartialViewResult GetMyAddress()  
  5. {  
  6.     return PartialView("_myAddress");  
  7. }  
So, we can see that with above syntax that PartialViewResult is returning a partial view from controller. This will be good if you are binding only single partial view. But if you have multiple partial views for single view then it will not good way to bind.

Third Way [Best Way, Always Use]

As per my knowledge the best way is to bind multiple partial views on one view to use ajax call and render all the partial view separately. Every partial view will make their own request for getting the data from database and bind with partial view.

If we process the separate request for the individual partial view then it will not affect the performance of the site. It is because this will not affect loading  the page. The page will be load and partial view will be trying to get the data asynchronously.

On the following example, I am going to show how to create multiple partial view and make separate request for getting the data from database. Basically I am getting Recent Post and Popular Post on the view,

View with Partial Views
  1. <div>  
  2.     <div class="panel panel-default text-left">  
  3.         <div class="panelheaderRed">  
  4.             Recent Articles  
  5.         </div>  
  6.         <div class="panel-body">  
  7.             <div id="dvHomeRecentPostBind">  
  8.                 <div class="loaderCenter">  
  9.                     <span class="loadingText">Recent Articles Loading....</span>  
  10.                 </div>  
  11.             </div>  
  12.         </div>  
  13.     </div>  
  14. </div>  
  15. <div>  
  16.     <div class="panel panel-default text-left">  
  17.         <div class="panelheaderGreen">  
  18.             Popular Articles  
  19.         </div>  
  20.         <div class="panel-body">  
  21.             <div id="dvHomePopularPostBind">  
  22.                 <div class="loaderCenter">  
  23.                     <span class="loadingText">Popular Articles Loading....</span>  
  24.                 </div>  
  25.             </div>  
  26.         </div>  
  27.     </div>  
  28. </div>  
My Controller
  1. public class RecentsController: BaseController  
  2. {  
  3.   
  4.     private readonly IPostRepository _postRepository;  
  5.     private readonly ICategoryRepository _categoryRepository;  
  6.   
  7.     public RecentsController(IPostRepository postRepository, ICategoryRepository categoryRepository)  
  8.         {  
  9.             _postRepository = postRepository;  
  10.             _categoryRepository = categoryRepository;  
  11.         }  
  12.         //  
  13.         // GET: /Recents/  
  14.   
  15.     [HttpGet]  
  16.     public ActionResult RecentPosts()  
  17.     {  
  18.         var postEntity = _postRepository.GetPosts().OrderByDescending(x => x.PostUpdatedDate).Take(5).Select(x => new PostViewModel()   
  19.         {  
  20.   
  21.             PostTitle = x.PostTitle,  
  22.                 PostAddedDate = x.PostAddedDate,  
  23.                 PostUrl = x.PostUrl,  
  24.                 postCategory = _categoryRepository.GetSingleCategoryInfo(x.CategoryId)  
  25.         });  
  26.   
  27.         return PartialView("RecentPosts", postEntity);  
  28.     }  
  29.   
  30.     [HttpGet]  
  31.     public ActionResult PopularPosts()  
  32.     {  
  33.         var postEntity = _postRepository.GetPosts().OrderByDescending(x => x.NumberOfViews).Take(5).Select(x => new PostViewModel()  
  34.            a{  
  35.             PostTitle = x.PostTitle,  
  36.                 PostAddedDate = x.PostAddedDate,  
  37.                 PostUrl = x.PostUrl,  
  38.                 postCategory = _categoryRepository.GetSingleCategoryInfo(x.CategoryId),  
  39.                 NumberOfViews = x.NumberOfViews  
  40.   
  41.         });  
  42.   
  43.         return PartialView("PopularPosts", postEntity);  
  44.     }  
  45.   
  46. }  
Jquery [Ajax Call]
  1. function LoadHomeRecentPost()  
  2. {  
  3.     $.ajax  
  4.     ({  
  5.         url: "/Recents/RecentPosts",  
  6.         contentType: "application/html; charset=utf-8",  
  7.         type: "GET",  
  8.         cache: !0,  
  9.         datatype: "html",  
  10.         success: function(t)  
  11.         {  
  12.             $("#dvHomeRecentPostBind").html(t)  
  13.         },  
  14.         error: function()  
  15.         {  
  16.             $("#dvHomeRecentPostBind").html("Post Not Found")  
  17.         }  
  18.     })  
  19. }  
  20.   
  21. function LoadHomePopularPost()  
  22. {  
  23.     $.ajax  
  24.     ({  
  25.         url: "/Recents/PopularPosts",  
  26.         contentType: "application/html; charset=utf-8",  
  27.         type: "GET",  
  28.         cache: !0,  
  29.         datatype: "html",  
  30.         success: function(t)  
  31.         {  
  32.             $("#dvHomePopularPostBind").html(t)  
  33.         },  
  34.         error: function()  
  35.         {  
  36.             $("#dvHomePopularPostBind").html("Post Not Found")  
  37.         }  
  38.     })  
  39. }  
  40.   
  41. $(document).ready(function()  
  42. {  
  43.     LoadHomeRecentPost(), LoadHomePopularPost()  
  44. });  
When you run the website, you can see clearly in the following image that the  website has been loaded but the content is still loading so, herethe website is not waiting for loading all the content of the site.

site

The part which is going to load, you can show the loading content message, if the request will get the response from the server than the data will be bound with partial view. So, using this you can make your asp.net mvc website faster. When your site will load properly then it will look like the following image with whole data.

data

Thanks for reading this article, hope you enjoyed it.
 
Read more articles on ASP.NET:

Up Next
    Ebook Download
    View all
    Learn
    View all