gpt4 book ai didi

c# - MVC 中绑定(bind) Controller 方法的问题

转载 作者:行者123 更新时间:2023-11-30 18:32:32 25 4
gpt4 key购买 nike

我正在实现一个具有两个导航超链接的页面:上一个和下一个。

第一个问题,每次我点击一个超链接,它都会调用第一次的 Action 。第二次开始,它停止调用 Controller 上的操作方法。我知道浏览器会缓存链接。所以我使用了代码 OutputCache... 但它仍然不起作用。

第二个问题是在单击超链接时操作方法被调用两次。

有人能告诉我我在这里缺少什么吗?对于经常使用 Asp.net 的人来说,这似乎很简单。我已经记下了我正在使用的代码。请帮忙。

Controller 代码:

 [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}&amp;blogId={1}",currentPage,blogId),
BlogNextLink = string.Format("/BloggerHome/NextPage?currentPage={0}&amp;blogId={1}",currentPage,blogId)
};
return Json(result,JsonRequestBehavior.AllowGet);
}

查看代码:

@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 方法:

function UpdateTitleClient() {
$.getJSON("BloggerHome/UpdateTitleServer", function (data) {
$("#blogTitle").html(data.Title);
$("#PrevPage").attr("href", data.BlogPrevLink);
$("#NextPage").attr("href", data.BlogNextLink);
});
}

最佳答案

请记住,getJSON() 是一种异步方法,因此如果您使用调试器单步执行代码,事件的顺序可能不太可预测。

getJSON() 没有 async:false 设置,所以只需使用 ajax,而不是将 async 设置为 false,将 dataType 设置为 json。如下所示:

 $.ajax({
dataType: "json",
url: yoururl,
async: false,
data: data,
success: function (data) {
$("#blogTitle").html(data.Title);
$("#PrevPage").attr("href", data.BlogPrevLink);
$("#NextPage").attr("href", data.BlogNextLink);
}

});

关于c# - MVC 中绑定(bind) Controller 方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18564076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com