【问题标题】:ASP.NET MVC RedirectToAction does not refresh the pageASP.NET MVC RedirectToAction 不刷新页面
【发布时间】:2019-09-12 09:34:18
【问题描述】:

我有控制器操作刷新,它只是更新当前页面。 但是当我通过 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 的新手,请不要判断强硬

编辑:到目前为止我做了什么来找出发生了什么。

我通过提琴手检查我是否从浏览器获得了缓存结果,或者我猜浏览器没有缓存问题,因为我得到了 http 200。

我在两个动作中都使用了这个属性[OutputCache(Location=System.Web.UI.OutputCacheLocation.None)] 没有帮助。

【问题讨论】:

  • 使用ajax你不能重定向RedirectToAction("Refresh");你可以使用location.href或者你可以使用return JavaScript("document.location.replace('" + Url.Action("Refresh", "Refresh") + "');");
  • 我不是直接从 ajax 调用重定向。之后我对我的 resetitem 操作进行了 ajax 调用,然后我将重定向操作设置为 refreshaction
  • 您的 RedirectToAction("Refresh"); 实际上将您的操作返回到您的 ajax 成功,您可以在您的 ajax 调用的 success 响应中以文本形式获得它

标签: c# asp.net-mvc entity-framework asp.net-mvc-4 model-view-controller


【解决方案1】:

您正在使用 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);
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多