-1

I have a datatable values converted to json which is passed to the client side. Now i have to make a loop of the result and add it to an array.

//Code:

 public string GetColumns()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Rows.Add("Id");
            dt.Rows.Add("First Name");
            dt.Rows.Add("Last Name");
            dt.Rows.Add("Mobile Number");
            dt.Rows.Add("Email");

            return JsonConvert.SerializeObject(dt);

        }

//ClientCode:

var colHeader = [];

$.ajax({
    url: '@(Url.Action("GetColumns", "Default"))',
    //url: 'Home/GetColumns',
    datatype: 'json',
    mtype: 'GET',
    success: OnComplete,
    error: OnFail
});
function OnComplete(result) {
    alert(result);
    $.each(result, function () {

        colHeader.push(this.Name);               

    });
}
  function OnFail(result) {
        alert('Failed');
    }

I'm getting the value inside the OnComplete function. Not sure how to loop and add it into an array colHeader.

Note: This is basically to bind the header of jqGrid dynamically.

A Coder
  • 3,039
  • 7
  • 58
  • 129

1 Answers1

0

Try below code. It should help.

function OnComplete(result) {
    alert(result);
    var parsed = JSON.parse(result);

    var colHeader= [];

    for(var x in parsed){
      colHeader.push(parsed[x]);
    }
}

Reference here

Community
  • 1
  • 1
ngrashia
  • 9,869
  • 5
  • 43
  • 58
  • @SanthoshKumar. Thanks for accepting as answer. It would be great if you upvote answers, since it would improve our `tag` score! Thanks – ngrashia Aug 07 '14 at 06:59