JQuery 和 Ajax.BeginForm() 两次发布相同的数据

标签 jquery ajax.beginform

好吧,我是一个完全的网络新手,但我必须承认我现在完全着迷了。这是我的问题:

我有一个带有 MVCContrib 网格的页面和一个“添加帐户”链接,该链接会打开一个包含在 JQuery 对话框中的 Ajax 表单。当我第一次完成工作流程时,一切都很好。我可以添加一个新项目并通过 JQuery/Ajax 刷新网格(所以我认为)。但是,当我尝试第二次添加表单时,始终会提交第一个表单中的数据。我已经关注这个问题太久了,必须承认我完全陷入困境。顺便说一句 - 我确信我这样做完全错误,所以请随意提出更好的建议。

表格如下:

@using (Ajax.BeginForm("SaveCheckAccount", "UserManagement", null, new AjaxOptions { OnSuccess = "onAccountAdded", OnFailure = "onAccountAddedFailed"}, new { id = "accountDetails" }))
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.version)
    @Html.HiddenFor(model => model.User_Id)
    @Html.HiddenFor(model => model.IsDefault)        
    <table>
        <tr>
            <td>
                Account Number
            </td>
            <td>
                @Html.TextBoxFor(model => model.AccountNumber)
                @Html.ValidationMessageFor(model => model.AccountNumber, "*")
            </td>
        </tr>
        <tr>
            <td>
                Routing Number
            </td>
            <td>
                @Html.TextBoxFor(model => model.RoutingNumber)
                @Html.ValidationMessageFor(model => model.RoutingNumber, "*")
            </td>
        </tr>
        <tr>
            <td>
                Account Type
            </td>
            <td>
                @Html.DropDownListFor(model => model.AccountType_Id, new SelectList(@accountTypes, "ID", "Name", Model.AccountType_Id))
                @Html.ValidationMessageFor(model => model.CheckAccountType)
                @Html.ValidationMessageFor(model => model.AccountType_Id, "*")
            </td>
        </tr>
        <tr>
            <td>
                Bank Name
            </td>
            <td>
                @Html.TextBoxFor(model => model.BankName)
                @Html.ValidationMessageFor(model => model.BankName, "*")
            </td>
        </tr>
        <tr>
            <td>
                Account Name
            </td>
            <td>
                @Html.TextBoxFor(model => model.AccountName)
                @Html.ValidationMessageFor(model => model.AccountName, "*")
            </td>
        </tr>
    </table>   
}

<script type="text/javascript">
    $.ajaxSetup({ cache: false });
</script>

此 JavaScript 驻留在bank.js 文件中

function BindCommands(createUrl) {

      $("#modalAdd").live("click", function (e) {
          var dialogBox = $("<div>");
          e.preventDefault();
          $(dialogBox).dialog({
              autoOpen: false,
              resizable: false,
              title: 'Create Account',
              modal: true,
              show: { effect: "blind", duration: 50 },
              hide: { effect: "blind", duration: 50 },
              open: function (event, ui) {
                  $.ajax(
                      {
                          type: "Get",                          
                          url: createUrl,
                          success: function (result) {
                              $(dialogBox).html(result);
                          }
                      });
              },

              buttons: {
                  Save: function () {
                      $("#accountDetails").submit();
                      $(this).dialog('close');                      
                  },
                  Cancel: function () {
                      $(this).dialog("close");
                  }
              }
          });
          $(dialogBox).dialog('open');
      });
  }

  function onAccountAdded(data, status, xhr) {      
      $("#accounts-grid").html(data);
  };

  function onAccountAddedFailed(data, status, xhr) {
      alert("Failed");      
  };

for 最初是通过单击此 View 上的添加链接来呈现的:

<script type="text/javascript">
    $.ajaxSetup({cache:false});
    $(document).ready(function() {
        var createUrl = '@Url.Action("NewBankAccountDetails", "UserManagement", new {userId=@Model.Id})';
        BindCommands(createUrl);
    });

</script>       
@if (Model != null && Model.Id > 0)
{

    <tr>
        <td class="header" colspan="2">
            User Accounts
        </td>
    </tr>
    <tr>
        <td>
            <a href="#" id="modalAdd">Add Account</a>
        </td>
    </tr>

    Html.RenderPartial("_BankAccountGrid", Model.CheckAccounts);
}

最佳答案

我遇到了同样的问题,并将我的头撞到墙上,直到我注意到以下脚本被包含两次:

<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>

我在我的包中设置了它的脚本,并且 View 模板也包含它,这导致了 ajax 表单的双重发布。

希望它能有所帮助,如果不是双倍赏金的话:-P

关于JQuery 和 Ajax.BeginForm() 两次发布相同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10017980/

相关文章:

javascript - 是否可以将事件传递给 Ajax 表单的 OnBegin 函数?

javascript - Ajax.BeginForm 强制完全回发

ajax - 防止在 Ajax.BeginForm 上重新加载

javascript - css 在输入字段上方显示验证消息

java - 如何使用 Ajax.beginform + 部分 View 更新我的表,在 ym asp.net mvc Web 应用程序内部

javascript - 在 jQuery 函数中多次调用 ColdFusion.Ajax.submitform()

jquery - 是否有一个免费的(LGPL 兼容)jQuery 网格插件,具有与 Ext-JS 网格类似的功能?

javascript - 为什么 Ajax.BeginForm 无法正常工作

javascript - 使用变量作为对象的属性名称 - Javascript

javascript - 使用javascript计算多个文本区域剩余字数