【问题标题】:View not refreshing after Ajax callAjax 调用后视图不刷新
【发布时间】:2014-01-04 05:35:54
【问题描述】:

我正在通过 Ajax 调用(我认为?)向我的控制器提交一种数据形式以进行处理。保存该行后,我希望重定向到最初加载表单的控制器中的原始 HttpGet 操作。

我发现 ajax 调用有效,控制器动作触发,数据保存到数据库中。但是,重新加载视图后屏幕永远不会刷新。

我在控制器中的操作上的“返回视图(模型)”上有一个断点,它会触发 - 但屏幕不会刷新。如果我使用 firebug 并查看 html,我会看到新行应该显示在我的视图中。但是,屏幕似乎根本没有重新加载。

我的 Javascript:

<script type="text/javascript">
        $(document).ready(function () {
            $('.btnSubmitNewCard').click(function () {
            var data = { cardNumber: $('.txtNewCardNumber').val(), cardHolder: $('.txtNewCardHolder').val(), expiryMonth: $('.txtNewExpiryMonth').val(), expiryYear: $('.txtNewExpiryYear').val(), active: $('.txtNewActive').val(), accountId: $('.Id').val() };

                $.ajax({
                    url: '@Url.Action("SaveBankCard", "BankAccount")',
                    type: "POST",
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    cache: false,
                    async: true,
                    success: function (result) {
                        console.log(result.toString());
                        if (result.Success == 'true') {
                            alert('Redirecting...');
                            window.location = '@Url.Action("EditBankAccount", "BankAccount", new {accountId = Model.Id})';
                        } 
                    },
                    error: function () {
                        alert("Oh no");
                    }

                });
            });
        });
    </script>

上面的javascript调用的控制器方法(成功):

public ActionResult SaveBankCard(string cardNumber, string cardHolder, int expiryMonth, int expiryYear, string active, int accountId)
{
    var card = new AccountCardDto
        {
            Id = 0,
            AccountId = accountId,
            Active = active == "on",
            CardHolderName = cardHolder,
            CardNumber = cardNumber,
            ExpiryDate = new DateTime(2000 + expiryYear, expiryMonth, 1)
        };

    int id = new BankAccountService().SaveCard(card);
    return RedirectToAction("EditBankAccount", new { bankAccountId = accountId });

}

然后是从“RedirectToAction”调用中调用的控制器动作:

       [HttpGet]
        [Authorize]
        [OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
        public ActionResult EditBankAccount(int? bankAccountId)
        {
            var model = new BankAccountModel();

            if (bankAccountId != null)
            {
....
            }


            return View(model);
}

最后一行,'return View(model)' 确实被调用了。如果我检查“模型”,我会看到持久化到数据库的新行。但是,正如我所说,屏幕不会刷新。

谁能告诉我为什么,以及如何修复/改善我的情况?

【问题讨论】:

  • result 在你的 ajax 成功回调中有哪些数据?
  • 我不是 100% 确定如何检查。你能帮忙吗?
  • 只需打开控制台(在浏览器上按 F12),您就会看到结果。
  • 看来我的结果总是“未定义”,因为我正在使用警报。我可以在调试模式下查看哪些选项卡?我有所有浏览器 - 哪个最好调试?

标签: javascript ajax asp.net-mvc jquery


【解决方案1】:

试试这个...你的方法SaveBankCard在控制器和ajax中调用EditBankAccount然后做一件事只在ajax或控制器中调用它

<script type="text/javascript">
        $(document).ready(function () {
            $('.btnSubmitNewCard').click(function () {
            var data = { cardNumber: $('.txtNewCardNumber').val(), cardHolder: $('.txtNewCardHolder').val(), expiryMonth: $('.txtNewExpiryMonth').val(), expiryYear: $('.txtNewExpiryYear').val(), active: $('.txtNewActive').val(), accountId: $('.Id').val() };

                $.ajax({
                    url: '@Url.Action("SaveBankCard", "BankAccount")',
                    type: "POST",
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    cache: false,
                    async: true,
                    success: function (result) {
                        console.log(result.toString());
                        if (result != 0) **//if your method return int else check it null if your method return string**
                        {
                            alert('Redirecting...');
                            window.location = '@Url.Action("EditBankAccount", "BankAccount", new {bankAccountId= Model.Id})'; **//change name of parameters**
                        } 
                    },
                    error: function () {
                        alert("Oh no");
                    }

                });
            });
        });
    </script>

控制器

public int SaveBankCard(string cardNumber, string cardHolder, int expiryMonth, int expiryYear, string active, int accountId)
{
    var card = new AccountCardDto
        {
            Id = 0,
            AccountId = accountId,
            Active = active == "on",
            CardHolderName = cardHolder,
            CardNumber = cardNumber,
            ExpiryDate = new DateTime(2000 + expiryYear, expiryMonth, 1)
        };

    int id = new BankAccountService().SaveCard(card);
    int bankAccountId = accountId;
    return bankAccountId ;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 2018-12-30
    相关资源
    最近更新 更多