gpt4 book ai didi

jquery ajax调用重新加载页面

转载 作者:行者123 更新时间:2023-12-01 07:59:53 25 4
gpt4 key购买 nike

我正在开发一个 asp.net mvc 页面,我将一个字符串传递给 Controller ​​ post 方法以获取记录并在 View 上填充。但是通过我的代码,我的 View 模型通过 ajax 调用获取了正确的记录,但 View 具有相同的旧记录数。我想我们必须在 jquery ajax 调用之后再次刷新它。您能对此提出一些想法吗?以下是我尝试过的。如果不符合标准,请随时建议代码更改。

$.ajax({
type: "POST",
url: "/Default/MyProjects",
data: { 'QuerySeperated': querySeperated },
success: function (result) {
//location.reload();
},
error: function (result) {

}
});



[HttpGet]
public virtual ActionResult MyProjects(int? id, string QuerySeperated)
{
var dataAccessHelper = new DataAccessHelper(true);
IList<test> myProjects;
if (QuerySeperated == null)
{
myProjects = dataAccessHelper.GetMyProjects(id);
}
else
{
myProjects = dataAccessHelper.GetMyProjects(id).Take(2).ToList();
}
var myProjectsViewModel = new MyProjectsViewModel() { GetMyProjects = myProjects };
return View(myProjectsViewModel);
}

[HttpPost]
public virtual ActionResult MyProjects(int? id, string QuerySeperated, string m)
{
var dataAccessHelper = new DataAccessHelper(true);
IList<test> myProjects = dataAccessHelper.GetMyProjects(id).Where(p => p.Title == "New Project").ToList();
var myProjectsViewModel = new MyProjectsViewModel() { GetMyProjects = myProjects };

return RedirectToAction("MyProjects", new { QuerySeperated });
}

最佳答案

您是对的,您必须刷新页面才能更新网络网格。一种方法是使用 ajax 进行重定向,而不是实际完成工作本身。将您的操作方法更改为如下所示:

[HttpPost]
public ActionResult MyProjects(int? id, string QuerySeperated, string m)
{
var redirectUrl = new UrlHelper(Request.RequestContext).Action("MyProjects", "ControllerName", new { QuerySeperated });
return Json(new { Url = redirectUrl });
}

这只会返回 get 方法的 url。您需要更改此设置,以便它也考虑到参数。然后将您的 ajax 调用更改为:

$.ajax({ type: "POST",
url: "/Default/MyProjects",
data: { 'QuerySeperated': querySeperated },
success: function (response) {
window.location.href = response.Url;
},
error: function () {
}
});

关于jquery ajax调用重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20796879/

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