3

I been struggling and looking everywhere but can not find solution to this. I want to pass Array data in $.ajax but don't know how to. 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 MVC3 controller?

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


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


        }

4 Answers 4

4

I would change your $.ajax call:

$.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 would take an array of those types:

public ActionResult So(BackerEntry[] backerEntries) 
{
    // ...
}
1
  • Thanks that is what I was mistaking
    – Pirzada
    Commented Jun 11, 2011 at 19:45
0

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

Try JSON.stringify() on client side to serialize data to string and then on server side try to decode that data.

You will get array or hash that you need.

JSON

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

$.ajax({ ...

data: { backerEntries: fields } 

... });

can U try this for serializing your Array?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.