【问题标题】:Refresh Index after redirecting to it from AJAX call从 AJAX 调用重定向到索引后刷新索引
【发布时间】:2015-05-06 12:02:39
【问题描述】:

我有一种情况,我正在设置用户值并尝试重新加载索引页面。这只是一个示例页面,我不能使用任何类型的用户控件,例如 ASP.NET。每个用户都在数据库中,并且从那里检索角色。我的索引是这样的:

 [HttpGet]
    public ActionResult Index(long? id)
    {
        AdminModel admin = new AdminModel();
        UserModel usermodel = new UserModel();
        if (id != null)
        {
            admin.UserModel = usermodel;
            admin.UserModel.UserId = id.ToString();
            admin.UserModel = UserAndRoleRepository.GetOrStoreUserProfile(admin.UserModel.UserId);
        }
        else
        {
            admin.UserModel = usermodel;
            admin.UserModel = UserAndRoleRepository.GetOrStoreUserProfile(currentUser);
        }

        return View(admin);
    }

第一次加载时效果很好。在页面中,我根据用户角色设置值:

 $(document).ready(function () {
    debugger;
    user = function () { return @Html.Raw(Json.Encode(Model)) }();
    if (user.UserModel != null) {
        if (user.UserModel.UserRole == 'ADMIN') {
            $("#btnAdmin").show();
            $("#btnTran").show();
            $("#btnNew").show();
            $("#btnAdjust").show();
            $("#btnReports").show();
        }
        if (user.UserModel.UserRole == 'TRANS') {
            $("#btnReports").show();
            $("#btnTran").show();
        }
        if (user.UserModel.UserRole == 'REPORTS') {
            $("#btnReports").show();
        }
    }
   });

AJAX 调用是这样的:

  $.ajax({
                type: 'POST',
                dataType: 'json',
                url: '@Url.Action("SetUser")',
                data: { userid: ui.item.value },
                success: function (data) {
                    if (data == null) {

                    }
                    else {

                    }

                },
                error: function (xhr) {
                    //var err = xhr.responseText;
                    //alert('error');
                }
            });

还有 SetUser 操作:

 [HttpPost]
    public ActionResult SetUser(string userid)
    {
        return RedirectToAction("Index", new { id = Convert.ToInt64(userid) });
    }

这很好用,因为 Index 方法是使用所选 ID 触发的,但页面不会重新加载以便能够设置按钮。有什么想法吗?

【问题讨论】:

  • 这是因为你有一个 ajax 调用。在ajax成功方法中,做location.reload(true) - More info
  • 重新加载只是以我作为用户再次加载我的索引,因为 ID 没有传递给索引。
  • @Dean.DePue 您找到解决方案了吗,还是需要帮助?
  • 不得不下班 - 明天将发布我所做的适合我独特情况的工作......
  • @Dean.DePue 有什么解决办法吗?以下答案对您有帮助还是您仍需要帮助?

标签: javascript c# jquery asp.net-mvc razor


【解决方案1】:

它不会重定向,因为您正在通过 ajax 调用返回一个操作。这里最好的做法是将userid 作为 JSON 返回,然后进行重定向。

所以 ajax success 函数将是:

            success: function (data) {
                if (data != null && data.UserID != null) {
                    location.href = '@(Url.Action("SetUser"))?userid=' + data.UserID;
                }
                else {
                    location.reload(); //something went wrong?
                }
            },
        });

你的行动是:

[HttpPost]
public JsonResult SetUser(string userid)
{
    return Json(new { UserID = Convert.ToInt64(userid) });
}

【讨论】:

  • 这不起作用,它只是重新加载具有相同用户 ID 的页面,我的。它应该使用新 ID 再次调用 index 方法,然后重新加载页面以设置按钮。
  • 你试过了吗? - location.href = '@Url.Action("SetUser",new { userid=" + data.UserID +" }' ;
  • @Dean.DePue 这与您的示例代码中概述的过程相同。我的代码背后的想法是,在 HttpPost 方法中,您发送用户 ID,然后导航到同一页面,但在查询字符串中使用新用户 ID(它使用新用户 ID 调用 index 方法)。这不是你想要的吗?
  • @AdershM 那行不通,你不能像这样互换地混合 JavaScript 和 Razor,因为 Razor 是在服务器上评估的,而 JavaScript 是在客户端上评估的。
  • 在我看来,剃须刀代码确实可以在 javascript 中工作,我已经尝试过了。当页面运行时,Url.Action 方法会扩展为有效的 url 链接,例如 /Contoller/Action?userid = 123。问题可能与查询字符串有关
【解决方案2】:

您需要进行 Ajax POST 调用吗?如果没有,您可以用

替换该代码
window.location.href = '@Url.Action("Index")' + '/' + ui.item.value

window.location.href = '@Url.Action("Index")' + '?id=' + ui.item.value

取决于您的路线图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多