I am implementing a page which has two navigation hyperlinks: Previous and next.
First problem, Every time I click on a hyperlink, it calls the action for the first time. Second time onwards, it stops calling the action method on the controller. I know that browser caches the link. So i used the code OutputCache...
but it still does not work.
Second problem is that the action method gets called twice on one click of the hyperlink .
Could someone tell me what am I missing here? It seems pretty simple for folks who have worked in Asp.net a lot. I have put down the code I am using. Please help.
Controller code:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*", Location = OutputCacheLocation.None)]
public string PreviousPage(int currentPage, int blogId){
List<Blog> blogs = db.Blogs.ToList();
List<Profile> profiles = db.Profiles.ToList();
var blog = blogs.FirstOrDefault(b => b.Id == blogId);
var detailsCount = blog.BlogDetails.Count();
if (currentPage == 0)
{
ViewBag.currentPage = Session["currentPage"]= currentPage;
}
else
{
ViewBag.currentPage =Session["currentPage"]= currentPage - 1;
}
ViewBag.blogId = Session["blogId"] = blogId;
ViewBag.blogTitle = Session["blogTitle"] = blog.Title;
if (blog.BlogDetails.Any())
{
return blog.BlogDetails[ViewBag.currentPage].BlogPage;
}
else {
return " ";
}
}
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*", Location = OutputCacheLocation.None)]
public string NextPage(int currentPage, int blogId){
List<Blog> blogs = db.Blogs.ToList();
List<Profile> profiles = db.Profiles.ToList();
var blog = blogs.FirstOrDefault(b => b.Id == blogId);
var detailsCount = blog.BlogDetails.Count();
if (currentPage == detailsCount - 1)
{
ViewBag.currentPage = Session["currentPage"] = currentPage;
}
else
{
ViewBag.currentPage = Session["currentPage"] = currentPage + 1;
}
ViewBag.blogId = blogId;
Session["blogTitle"] = blog.Title;
if (blog.BlogDetails.Any())
{
return blog.BlogDetails[ViewBag.currentPage].BlogPage;
}
else
{
return " ";
}
}
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public JsonResult UpdateTitleServer(){
var title = Session["blogTitle"];
int blogId = (int)Session["blogId"];
var currentPage = (int)Session["currentPage"];
var result = new {
Title = title.ToString(),
BlogPrevLink = string.Format("/BloggerHome/PreviousPage?currentPage={0}&blogId={1}",currentPage,blogId),
BlogNextLink = string.Format("/BloggerHome/NextPage?currentPage={0}&blogId={1}",currentPage,blogId)
};
return Json(result,JsonRequestBehavior.AllowGet);
}
View code:
@Ajax.ActionLink("<----", "PreviousPage","BloggerHome", new { currentPage = ViewBag.currentPage, blogId = ViewBag.blogId }, new AjaxOptions() {HttpMethod="Post", OnComplete="UpdateTitleClient", UpdateTargetId = "contentPanel" }, new {Id="PrevPage"})
@Ajax.ActionLink("---->", "NextPage","BloggerHome", new { currentPage = ViewBag.currentPage, blogId = ViewBag.blogId }, new AjaxOptions() {HttpMethod="Post",OnComplete="UpdateTitleClient",UpdateTargetId="contentPanel" },new {Id="NextPage"});
JavaScript method:
function UpdateTitleClient() {
$.getJSON("BloggerHome/UpdateTitleServer", function (data) {
$("#blogTitle").html(data.Title);
$("#PrevPage").attr("href", data.BlogPrevLink);
$("#NextPage").attr("href", data.BlogNextLink);
});
}