最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - passing array from javascript to controller MVC 4 - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Aug 7, 2013 at 19:53 laitha0laitha0 4,33611 gold badges35 silver badges50 bronze badges 4
  • 1 Side note - You don't need to call JSON.stringify, jQuery (or XHR2) will do this for you. – Benjamin Gruenbaum Commented Aug 7, 2013 at 19:56
  • did you try adding traditional: true to your ajax options? – lbstr Commented Aug 7, 2013 at 19:57
  • still same problem, it shows the right number of elements but they are all null – laitha0 Commented Aug 7, 2013 at 19:59
  • Have you tried with string[] instead? – Raciel R. Commented Aug 7, 2013 at 20:05
Add a comment  | 

4 Answers 4

Reset to default 11

the Ajax parameter

traditional : true

will do the trick.

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"); }
});

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)
{
}

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
            }
        });
发布评论

评论列表(0)

  1. 暂无评论