How Ajax is passed to the background array parameter

Source: Internet
Author: User

Originating From: http://blog.csdn.net/lingxyd_0/article/details/10428785

Bulk deletion and batch change status are used on the project, the Easyui DataGrid is used in the foreground, and the batch change data state function is used.

You can get the ID of each data in the foreground, but how do you pass it to the background in an array way?

Through the various ways of debugging last night, finally arrived at the answer! Here as a memo.

There are two ways of doing this:

Way One

Front Code:

Bulk Deletion and batch change status are used on the project, the Easyui DataGrid is used in the foreground, and the batch change data state function is used. You can get the ID of each data in the foreground, but how do you pass it to the background in an array way? Through the various ways of debugging last night, finally arrived at the answer! Here as a memo. There are two ways to do this: the way a foreground code: [JavaScript] View plain copy//Way Onevar_list = {};  for(vari = 0; i < checkedrow.length; i++) {_list["selectedids[" + i + "]"] =checkedrow[i].id; } $.ajax ({URL:' @Url. Action ("Setcallbackstatus") ',      //data: {"Selectedids": _list},data: _list, DataType:"JSON", type:"POST",      //Traditional:true,Successfunction(Responsejson) {//your logicAlert (' Ok ');  }  }); Note:1, _list is an object2. The attributes in _list need to be combined with the background parameter name, such as "Selectedids", combined into similar: selectedids[0],selectedids[1]... Wait for Request.params here is the most important, otherwise the backstage does not recognize. This way you can also pass an array of custom classes. Combination means selectedids[ .0]. Firstname,selectedids[0]. LASTNAME,SELECTEDIDS[1]. Firstname,selectedids[1]. LastName ...3, the Ajax data parameter is directly specified as _list background code:
 Public ActionResult setcallbackstatus (list<int> selectedids)      {          = "OK";           = "";             return this. Jsonformat (new {result = result, ErrMsg = errmsg});      
Way two front desk code:
var_list = [];  for(vari = 0; i < checkedrow.length; i++) {_list[i]=checkedrow[i].id; } $.ajax ({URL:' @Url. Action ("Setcallbackstatus") ', data: {"Selectedids": _list},//data: _list,DataType: "JSON", type:"POST", Traditional:true, Success:function(Responsejson) {//your logicAlert (' Ok '); }  });

Note:1, _list is an array. 2. Thedata in the Ajax parameter is {"Selectedids": _list}3, this method is more important traditional:true. or convert the _list parameter in 2 to $.param (_list,true). In fact, this is the _list as the traditional way to pass to the backstage. jquery is converted by default. It is said to use PHP .... Background language. In fact, it automatically appends "[]" to the parameter. Background code: The same way one for custom classes, can also be passed by way of a jquery Ajax to the backend for example:
//Customizing the Person class Public classPerson { Publicstring FirstName {get; set;}  Publicstring LastName {get; set;} }//Background Action PublicActionResult Setcallbackstatus (list<person>selectedids) {string result= "OK"; String ErrMsg= ""; return  This. Jsonformat (New{result = result, ErrMsg =errmsg}); } 

At this time the front desk JS can write:
var_list = {};  for(vari = 0; i < checkedrow.length; i++) {_list["selectedids[" + i + "]. FirstName "] =Checkedrow[i].      FirstName; _list["selectedids[" + i + "]. LastName "] =Checkedrow[i].  LastName; } $.ajax ({URL:' @Url. Action ("Setcallbackstatus") ',      //data: {"Selectedids": _list},data: _list, DataType:"JSON", type:"POST",      //Traditional:true,Successfunction(Responsejson) {//your logicAlert (' Ok ');  }  }); 

Attention:

1. _list is an object

2, the attributes in _list need to combine the background parameter name, for example "Selectedids", combined into similar: selectedids[0],selectedids[1] ... ET request.params

Here is the most important, otherwise the background does not recognize. This way you can also pass an array of custom classes. The combination is selectedids[0]. Firstname,selectedids[0]. LASTNAME,SELECTEDIDS[1]. FIRSTNAME,SELECTEDIDS[1]. LastName ...

3. The data parameter of AJAX is directly specified as _list


Background code:

[CSharp]View plain copy
  1. Public ActionResult setcallbackstatus (list<int> selectedids)
  2. {
  3. string result = "OK";
  4. string errmsg = "";
  5. return this .  Jsonformat (new {result = result, ErrMsg = errmsg});
  6. }


Way Two

Front Code:

[JavaScript]View plain copy
  1. var _list = [];
  2. for (var i = 0; i < checkedrow.length; i++) {
  3. _list[i] = checkedrow[i].id;
  4. }
  5. $.ajax ({
  6. URL: ' @Url. Action ("Setcallbackstatus") ',
  7. Data: { "selectedids": _list},
  8. //data: _list,
  9. DataType: "JSON",
  10. Type: "POST",
  11. Traditional: true,
  12. Success: function (Responsejson) {
  13. //Your logic
  14. Alert (' Ok ');
  15. }
  16. });


Attention:

1, _list is an array.

2. The data in the Ajax parameter is {"Selectedids": _list}

3, this way is more important traditional:true. or convert the _list parameter in 2 to $.param (_list,true). In fact, this is the _list as the traditional way to pass to the backstage. jquery is converted by default. It is said to use PHP .... Background language. In fact, it automatically appends "[]" to the parameter.

Background code:

Same way One

For custom classes, you can also pass the way a jquery Ajax to the background

For example:

[CSharp]View plain copy
    1. Customizing the Person class
    2. Public class person
    3. {
    4. public string FirstName { get;  set;}
    5. public string LastName { get;  set;}
    6. }
[CSharp]View plain copy
    1. Background action
    2. Public ActionResult Setcallbackstatus (list<person> selectedids)
    3. {
    4. string result = "OK";
    5. string errmsg = "";
    6. return this .  Jsonformat (new {result = result, ErrMsg = errmsg});
    7. }


At this time the front desk JS can write:

[JavaScript]View plain copy
    1. var _list = {};
    2. for (var i = 0; i < checkedrow.length; i++) {
    3. _list["selectedids[" + i + "]. FirstName "] = Checkedrow[i].  FirstName;
    4. _list["selectedids[" + i + "]. LastName "] = Checkedrow[i].  LastName;
    5. }
    6. $.ajax ({
    7. URL: ' @Url. Action ("Setcallbackstatus") ',
    8. //data: {"Selectedids": _list},
    9. Data: _list,
    10. DataType: "JSON",
    11. Type: "POST",
    12. //traditional:true,
    13. Success: function (Responsejson) {
    14. //Your logic
    15. Alert (' Ok ');
    16. }
    17. });

How Ajax is passed to the background array parameter

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.