ASP.NET MVC RedirectToAction不刷新页面

时间:2019-04-23 08:29:39

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

我有控制器动作“刷新”,它仅更新当前页面。 但是当我通过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)] 中都使用了此属性 没有帮助。

1 个答案:

答案 0 :(得分:2)

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