ASP.NET MVC RedirectToAction 不刷新页面
ASP.NET MVC RedirectToAction does not refresh the page
我有控制器操作 Refresh,它只更新当前页面。
但是当我通过 RedirectoAction 方法调用该操作时,我遇到了问题,页面没有更新。我必须在那之后按下刷新按钮来独立调用刷新操作,以获得所需的结果。
这是我的客户端代码。这调用了我的 ResetItems 操作,该操作又重定向到 Refresh 操作。
function ResetSelectedItems() {
var guidId = $("#guidId")[0].value;
console.log(guidId[0].value);
$.ajax({
type: 'POST',
url: '/UploadFile/ResetItems',
data: { guidId : guidId},
}
)
}
[HttpPost]
[ActionName("ResetItems")]
public ActionResult ResetItems(string guidId)
{
//Some logic here updating in db etc..
return RedirectToAction("Refresh");
}
[ActionName("Refresh")]
public ActionResult Refresh(int? id)
{
//Refresh logic which eventually render refresh the current view
}
另外我想提一下,在这个项目中我们使用了 IUnitOfWork 模式,它会以某种方式导致这种意想不到的结果吗?
P.S我是新手ASP.NET请不要评判强硬
编辑:到目前为止我做了什么来找出发生了什么。
我通过 fiddler 检查是否从浏览器获得了缓存结果,或者我猜浏览器没有缓存问题,因为结果是 http 200。
我在两个动作中都使用了这个属性[OutputCache(Location=System.Web.UI.OutputCacheLocation.None)]
没有帮助。
您正在使用 AJAX 请求,这意味着无论响应如何,html 页面都不会重新加载。
我猜你需要这样的东西:
[HttpPost]
[ActionName("ResetItems")]
public ActionResult ResetItems(string guidId)
{
//Some logic here updating in db etc..
//string redirectUrl = Url.Action("Refresh", new { id = "your int id" });
string redirectUrl = Url.Action("Refresh");
return Json(new { redirectUrl });
}
$.ajax({
type: 'POST',
url: '/UploadFile/ResetItems',
data: { guidId : guidId},
success: function (response) {
window.location.replace(response.redirectUrl);
}
});
我有控制器操作 Refresh,它只更新当前页面。 但是当我通过 RedirectoAction 方法调用该操作时,我遇到了问题,页面没有更新。我必须在那之后按下刷新按钮来独立调用刷新操作,以获得所需的结果。
这是我的客户端代码。这调用了我的 ResetItems 操作,该操作又重定向到 Refresh 操作。
function ResetSelectedItems() {
var guidId = $("#guidId")[0].value;
console.log(guidId[0].value);
$.ajax({
type: 'POST',
url: '/UploadFile/ResetItems',
data: { guidId : guidId},
}
)
}
[HttpPost]
[ActionName("ResetItems")]
public ActionResult ResetItems(string guidId)
{
//Some logic here updating in db etc..
return RedirectToAction("Refresh");
}
[ActionName("Refresh")]
public ActionResult Refresh(int? id)
{
//Refresh logic which eventually render refresh the current view
}
另外我想提一下,在这个项目中我们使用了 IUnitOfWork 模式,它会以某种方式导致这种意想不到的结果吗?
P.S我是新手ASP.NET请不要评判强硬
编辑:到目前为止我做了什么来找出发生了什么。
我通过 fiddler 检查是否从浏览器获得了缓存结果,或者我猜浏览器没有缓存问题,因为结果是 http 200。
我在两个动作中都使用了这个属性[OutputCache(Location=System.Web.UI.OutputCacheLocation.None)]
没有帮助。
您正在使用 AJAX 请求,这意味着无论响应如何,html 页面都不会重新加载。 我猜你需要这样的东西:
[HttpPost]
[ActionName("ResetItems")]
public ActionResult ResetItems(string guidId)
{
//Some logic here updating in db etc..
//string redirectUrl = Url.Action("Refresh", new { id = "your int id" });
string redirectUrl = Url.Action("Refresh");
return Json(new { redirectUrl });
}
$.ajax({
type: 'POST',
url: '/UploadFile/ResetItems',
data: { guidId : guidId},
success: function (response) {
window.location.replace(response.redirectUrl);
}
});