2012-04-04 2 views
11

좋아요. 나는 총 웹 초보자입니다.하지만 지금은 완전히 매료 된 걸 인정해야합니다. 여기에 내 문제가 :JQuery와 Ajax.BeginForm()이 동일한 데이터를 두 번 게시합니다.

MVCContrib 표 및 JQuery 대화 상자에 래핑 된 Ajax 폼을 가져 오는 "계정 추가"링크가있는 페이지가 있습니다. 워크 플로우를 처음 시작할 때 모든 것이 잘 작동합니다. 새 항목을 추가하고 JQuery/Ajax를 통해 그리드를 새로 고칠 수 있습니다 (그래서 생각합니다). 그러나 양식을 두 번 추가하려고하면 첫 번째 양식의 데이터가 항상 제출됩니다. 나는 그 문제를 너무 오랫동안보고 있었고 나는 완전히 붙어 있다는 것을 인정해야만한다. Btw - 나는 이것을 완전히 잘못하고 있다고 확신하므로 더 나은 것을 제안 할 자유를 느낀다.

@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> 

이 자바 스크립트가 bank.js에있는 intially이보기에 추가 링크를 클릭하여 렌더링을 위해

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");  
    }; 

파일 :

<script type="text/javascript"> 
    $.ajaxSetup({cache:false}); 
    $(document).ready(function() { 
     var createUrl = '@Url.Action("NewBankAccountDetails", "UserManagement", new {[email protected]})'; 
     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); 
} 

답변

35
다음

폼의

나는 다음과 같은 스크립트가 포함 된 것을 알아 채기 전까지 같은 문제가 있었고 벽에 머리를 두드렸다. 두 번 uded :

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

나는 스크립트 내 번들 내부 설정하고 뷰 템플릿은 또한 아약스 양식에 의해 이중 포스트의 결과를 포함했다.

(나는 어떤 reaso마다 두 개의 파일을 볼 수있는 디버거로) 솔직히이 내 문제를 바랐다 현상금 - P

+0

을 두 배로하지만하지하지 않는 경우는, 도움이되기를 바랍니다. – Worthy7

+0

오늘을 절약하기위한 Thx! –

+1

이 답변은 올바른 것으로 표시되어야합니다. – AlphaTry

관련 문제