我正在使用asp.net mvc页面,我将一个字符串传递给控制器post方法以获取记录并填充视图。但是使用我的代码我的viewmodel使用ajax调用获取正确的记录,但是视图具有相同的旧记录数。我想我们必须在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 });
}
答案 0 :(得分:2)
您必须刷新页面才能更新到webgrid。这样做的一种方法是使用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 () {
}
});