Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

jquery - AJAX Post of JavaScript String Array to JsonResult as List<string> Always Returns Null?

I'm trying to build up a string array in JavaScript and get the results in a string list in the action method. Below is what my JavaScript looks like. I'm using jQuery 1.4.2. The problem is my List in the action method is always showing NULL. Will a JavaScript string array not map correct to a string list in C#?

var test = ['test1', 'test2'];
var parms = {
  var1: 'some string',
  var2: test
};

$.ajax({
  type: "POST",
  url: "/Test/JSONTestAction",
  async: false,
  data: parms,
  dataType: "json",
  success: function(data) {

    // success
  }
});

Then my JsonResult looks like the following:

public JsonResult JSONTestAction(string var1, List <string> var2) {
 // var2 is always NULL -- not good

 return Json(new {
  test = "test"
 });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I faced the same problem after updating to jquery 1.4.2. You can find the solution here (in the Ajax section).

Adding traditional : true in the ajax options should work.

$.ajax({
    type: "POST",
    traditional: true,
    url: "/Test/JSONTestAction",
    async: false,
    data: parms,
    dataType: "json",
    success: function(data) {

        // success
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...