2

I am using razor and I'm having a hard time passing an array to a controller. the array contains objects that I made and I am trying to do this:

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: JSON.stringify(operationCollection),
     success: function (data) { alert("SUCESS");},
     dataType: "json",
     contentType: "application/json"
});

and my controller is:

 public void HandleOperations(List<string> operationCollection)
 {

 }

I am not required to use ajax but I am not sure how else it could be done. In the controller it shows that the "operationCollection" contains elements but they are all null.

laitha0
  • 4,148
  • 11
  • 33
  • 49

4 Answers4

11

the Ajax parameter

traditional : true

will do the trick.

Nalan Madheswaran
  • 10,136
  • 1
  • 57
  • 42
  • What do you mean? Where should I write traditional?? – radbyx Dec 05 '13 at 14:50
  • 1
    $.ajax({ type: "POST", url: "HomePage/HandleOperations", data: {operations: operationCollection}, traditional: true, success: function (data) { alert("SUCCESS"); } }); – puddinman13 Dec 20 '13 at 20:18
4

Usage of the traditional: true parameter for an ajax call:

To help radbyx, using the "traditional: true" property of an ajax call, like the following, will tell ajax to use the traditional form of serialization. More details: http://api.jquery.com/jQuery.param/ or What is "traditional style of param serialization' in JQuery.

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: {operations: operationCollection},
     traditional: true,
     success: function (data) { alert("SUCCESS"); }
});
Community
  • 1
  • 1
puddinman13
  • 1,408
  • 4
  • 18
  • 35
3

Client side:

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: {operations: operationCollection},
     success: function (data) { alert("SUCCESS"); }
});

and declare a class server side like this:

public class Operation
{
  public int Index;
  public string Source;
  public string Target;
  public int AddOrDel;
}

then you can have this action:

public void HandleOperations(Operation[] operations)
{
}
Anders Lindén
  • 6,839
  • 11
  • 56
  • 109
  • this works! I did not know that I need a model for it. Thanks – laitha0 Aug 07 '13 at 20:47
  • Well, need and need, you will get the opportunity to put words on the items, they are after all operations :) – Anders Lindén Aug 07 '13 at 20:48
  • 1
    In the json version, you keep the call to JSON.Stringify, and dataType: "json" and have an action that takes a string as parameter, public void HandleOperations(string operations), then you call var operationArr = JavaScriptSerializer.Deserialize(operations);, you need to add a reference to System.Web.Extensions to your project – Anders Lindén Aug 07 '13 at 20:54
  • 3
    you actually dont need a model, using traditional:true will do it in ajax post. – Sakthivel Sep 25 '13 at 07:26
-1

Add "traditional:true" parameter in your ajax.

Example:

var parentValueToPush=new Array();
$('[name=SelectedUsers]:checked').each(function () {
            parentValueToPush.push($(this).val());
            temp.push($(this).val());
        });
        $.ajax({
            url: //URL,
            type: 'get',
            traditional: true,
            dataType: 'html',
            cache: false,
            data: { SelectedUsers: parentValueToPush},
            success: function (data) {
                   //Result
            }
        });
Mohammad Imran
  • 125
  • 1
  • 6