JQuery Ajax to pass an array. Help is needed

I fought and looked everywhere, but cannot find a solution to this. I want to pass array data to $ .ajax, but don’t know how to do it. Below is the code.

$("#procressGrid").click(function () {
         var dataArray = [];
         $.each($(".gridTable tr:not(.gridTitleRow)"), function(a, b){
                var id = $("input.idField", b).val();
                var code = $("input[name='code']", b).val();

                dataArray.push({
                    "id": id,
                    "code": code
                })

         });

         $.ajax({
                url: "/HeaderMenu/So",
                type: "POST",

                data: { backerEntries[]: dataArray } 

                dataType: "json",
                contentType: "application/json; charset=utf-8",
                beforeSend: function () { $("#statusDropdown").fadeIn(); },
                complete: function () { $("#statusDropdown").fadeOut(); },
                success: function (data) {

                    if (data.Status == "Success") {

                    } else {

                    }
                },
                error: function () {
                    $("#error").show().html("An error has occured!!!");
                }
            });
    });

and what to declare in the MVC3 controller?

 public ActionResult So(Array backerEntries)
        {
            //var b = a;


                return Json(new { status = "Success", message = "Passed" });


        }
+3
source share
4 answers

I would change your call $.ajax:

$.ajax({
   /*snip */
   data: dataArray
}); 

And on the server side, create a view model to bind to:

public class BackerEntry
{
    public string Id { get; set; }
    public string Code { get; set; }
}

Now your action will take an array of the following types:

public ActionResult So(BackerEntry[] backerEntries) 
{
    // ...
}
+4
source

Will this work for you?

 var dataArray = new Array();

 // fill your array     

 $.ajax({
     url: "/HeaderMenu/So",
     type: "POST",
     data: {'backerEntries' : dataArray},
     // the rest of your code
 });
0
source

JSON.stringify() , .

, .

JSON

0
  var fields = $("
      .gridTable tr:not(.gridTitleRow) input.idField,
      .gridTable tr:not(.gridTitleRow) input[name='code']
").serializeArray();

$. Ajax ({ ...

data: { backerEntries: fields } 

...  });

U ?

0

All Articles