Binding all the partial views into one request. Try to get all the partial view data as an entity on an action result and restore the view with the model data. As in the following example, you can see that I need the latest Article and the Popular Article
public class ArticleViewModel
{
public List RecentArticle { get; set; };
public List PopularArticle { get; set; };
}
But on the controller, I have utilized only one Action Result to get the data for both views.
public ActionResult GetArticle()
{
var model = new ArticleViewModel();
model.RecentArticle = _db.getRecentArticleList();
model.PopularArticle = _db.getPopularArticleList();
return View(model);
}
It reloads the whole code in a single view, I transfer multiple partial views where data appears in a single request.
@model PartialViewDemo.ViewModels.ArticleViewModel
@Html.RenderPartial("RecentPostPartialView", model.RecentArticle);
@Html.RenderPartial("PopularPostPartialView", model.PopularArticle);
So as per the performance aspect, this way of using a partial view is not good. If we bind a single view it will not affect our website on a performance basis.
The best way is to tie multiple partial views into a single view using an Ajax call and provide all the partial views separately. Every partial view will make its request to retrieve data from the database and bind it with a partial view. Using an Ajax call, we can upload data without refreshing our page.
If we process a separate request for each view, then it will not affect the performance and loading of the site. The page will be loaded and the partial view will try to get the details in sync.
In this example, you create multiple partial views and make a separate request to retrieve data from the database.
My View
Recent Articles Loading....
Popular Articles Loading....
My Controller
public class RecentsController : Controller
{
private readonly IPostRepository _postRepository;
private readonly ICategoryRepository _categoryRepository;
public RecentsController(IPostRepository postRepository, ICategoryRepository categoryRepository)
{
_postRepository = postRepository;
_categoryRepository = categoryRepository;
}
[HttpGet]
public ActionResult RecentArticles()
{
var postEntity = _postRepository.GetPosts().OrderByDescending(x => x.PostUpdatedDate).Take(3).Select(x => new PostViewModel()
{
PostTitle = x.PostTitle,
PostAddedDate = x.PostAddedDate,
PostUrl = x.PostUrl,
postCategory = _categoryRepository.GetSingleCategoryInfo(x.CategoryId)
});
return PartialView("RecentArticles", postEntity);
}
[HttpGet]
public ActionResult PopularArticles()
{
var postEntity = _postRepository.GetPosts().OrderByDescending(x => x.NumberOfViews).Take(3).Select(x => new PostViewModel()
{
PostTitle = x.PostTitle,
PostAddedDate = x.PostAddedDate,
PostUrl = x.PostUrl,
postCategory = _categoryRepository.GetSingleCategoryInfo(x.CategoryId),
NumberOfViews = x.NumberOfViews
});
return PartialView("PopularArticles", postEntity);
}
}
Jquery For Ajax
function LoadRecentArticle()
{
$.ajax
({
url: "/Recents/RecentArticles",
contentType: "application/html; charset=utf-8",
type: "GET",
datatype: "html",
success: function(data)
{
$("#RecentArticle").html(data)
},
error: function()
{
$("#RecentArticle").html("Post Not Found")
}
})
}
function LoadPopularArticle()
{
$.ajax
({
url: "/Recents/PopularArticles",
contentType: "application/html; charset=utf-8",
type: "GET",
datatype: "html",
success: function(data)
{
$("#PopularArticle").html(data)
},
error: function()
{
$("#PopularArticle").html("Post Not Found")
}
})
}
$(document).ready(function()
{
LoadRecentArticle(), LoadPopularArticle()
});
By using this method, you can increase the performance of your website. The website gets fully loaded rather than waiting for the whole content to completely load.