2017-09-26 2 views
1

Meine Anwendung hat mehrere Daten wie Text-Box-Wert, Label-Wert, Drop-Down-Wert, Datei Daten und sende gleiche Daten auf der Serverseite und speichern in der DatenbankWie senden Sie mehrere Daten in einem einzelnen FormData-Objekt in Ajax?

Um dies zu erreichen, habe ich unten Methode aber jetzt i möchte Formdata verwenden und jeden Wert anhängen und an die Serverseite senden

So erreichen Sie dieses Szenario mithilfe von Formdata mit spezifischem Objekttyp.

ist unter dem Code

 var selectedText = $('#Commentinput').text(); 
     $('#actioncomments').text(selectedText); 
     var debitEntityValue = $('#DrAccount option:selected').text(); 
     var creditEntityValue = $('#CrAccount option:selected').text(); 
     var amount = $("#Amountinput").val(); 
     var paymentActionReason = $('#action').text(); 
     var paymentCommentReason = $('#Commentinput').val(); 
     var prepayAccountId [email protected]; 
     var transactionDate = '@DateTime.Today'; 
     var transactionExtensions = "1"; 
     var fileBase64Data = $("#fileUpload").text(); 
     if ($('#Commentinput').val() == "") { 
      paymentCommentReason = "No Comment"; 
     } 
     else { 
      paymentCommentReason = $('#Commentinput').val(); 
     } 
     var adjustmentTransactioninfo = 
     { 
      PaymentReasonMasterId: paymentReasonMasterId, 
      DebitEntityValue: debitEntityValue, 
      CreditEntityValue: creditEntityValue, 
      Amount: amount, 
      PaymentActionReason: paymentActionReason, 
      PaymentCommentReason: paymentCommentReason, 
      PrepayAccountId: prepayAccountId, 
      TransactionDate: transactionDate, 
      TransactionExtensions: transactionExtensions 
     }; 
     var data = JSON.stringify({ 
      'adjustmentTransactioninfo': adjustmentTransactioninfo, 
      'fileData': 0 
     }); 
     var url = "@Html.Raw(Url.Action("AdjustmentTransaction", 
     "PrepaidActivity"))"; 
     url += '?branchCode=' + '@Model.prepaidBranchList.IASBranchCode' 
     $.ajax({ 
      url: url, 
      traditional: true, 
      data: data, 
      enctype:"multipart/form-data", 
      contentType: "application/json charset=utf-8", 
      dataType: "json", 
      type: 'POST', 
      success: function (data) { 
       $("#ajaxLoader").show(); 
       if (data != null) { 

        var ProductActionID = data.SuccessMessage.split(':'); 
        uploadFile.append('ProductActionID' , ProductActionID[1]); 
        FileUpload(uploadFile); 
        var dialog = document.querySelector('#Finaldialog'); 
        var ConfirmationScreen = $("<p></p>").text(data.SuccessMessage); 
        $("#finalmdl-dialog").append(ConfirmationScreen); 
        dialog.showModal(); 
        dialog.querySelector('button:not([disabled])').addEventListener('click', function() { 
         dialog.close(); 
         location.reload(); 
        }); 
       } 
      } 
     });` 

Controller-Code

public JsonResult AdjustmentTransaction(TraxAdjustmentTransactionInfo adjustmentTransactioninfo, string fileData, string branchCode) 
    { 
     PrepaidAdminService.PrepaidAdminDashBoardServiceClient _PrepaidAdminService = new PrepaidAdminService.PrepaidAdminDashBoardServiceClient(); 

     TraxAdjustmentTransactionResult result; 
     adjustmentTransactioninfo.ProductActionMasterId = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["ProductActionMasterId"]); 
     adjustmentTransactioninfo.ProductCode = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ProductCode"]); 
     adjustmentTransactioninfo.DebitEntityMasterId = TraxEntityType.GPLedgerAccount; 
     adjustmentTransactioninfo.CreditEntityMasterId = TraxEntityType.GPLedgerAccount; 
     adjustmentTransactioninfo.UserId = Utility.UserID; 
     adjustmentTransactioninfo.RetailerId = _PrepaidAdminService.GetBranchRetailerId(branchCode); 
+0

Verwendung jQuery ("Form") .serializeArray() https: //api.jquery. com/serializeArray – AZinkey

+0

Jedes spezifische Beispiel –

Antwort

0

Verwendung serializeArray und kann die zusätzlichen Daten werden hinzufügen:

var data = $('form').serializeArray(); 
    data.push({name: 'Amit', value: 'love'}); 
    $.ajax({ 
     type: "POST", 
     url: "url", 
     data: data, 
     success: function(data) { 
     // do what ever you want with the server response 
    }, 
    error: function() { 
     alert('error handing here'); 
    } 
}); 
+0

Wie die gleichen Daten auf der Steuerungsseite erhalten. –

+0

Wenn eine Anforderung an eine ASP.NET-Anwendung gestellt wird, füllt der unformatierte Inhalt dieser Anforderung die Request.InputStream-Eigenschaft. Sie können auf diese Rohdaten zugreifen, indem Sie einen StreamReader verwenden: https://www.mikesdotnetting.com/article/220/posting-data-with-jquery-ajax-in-asp-net-razor-web-pages – AZinkey

0

Entfernen Sie die @Html.Raw(Url.Action()), contentType und JSON.stringify() und verlassen das zu der ModelBinder . Ändern Sie Ihre ajax dazu:

$.ajax({ 
    url: '@Url.Action("AdjustmentTransaction", "PrepaidActivity")', 
    traditional: true, 
    data: { 
     adjustmentTransactioninfo: adjustmentTransactioninfo, 
     fileData: "0", 
     branchCode: '@Model.prepaidBranchList.IASBranchCode' 
    }, 
    enctype: "multipart/form-data", 
    type: 'POST', 
    success: function (data) { 
     alert("success"); 

    } 
}); 
-1
var params = { param1: value, param2: value2} 
     $.ajax({ 
      type: 'POST', 
      contentType: 'application/json; charset=utf-8', 
      url: '../aspxPage.aspx/methodName', 
      data: JSON.stringify(params), 
      datatype: 'json', 
      success: function (data) { 
      var MethodReturnValue = data.d 

      }, 
      error: function (xmlhttprequest, textstatus, errorthrown) { 
       alert(" conection to the server failed "); 
      } 
     }); 

// Param1 und param2 Name sollte gleich wie Methode Argument

Verwandte Themen