How Ajax is passed to the background array parameter

Source: Internet
Author: User

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.

There are two ways of doing this:

Way One

Front Code:

Way One  var _list = {};    for (var i = 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,      success:function (Responsejson) {          //your Logic          alert (' 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:

Public ActionResult Setcallbackstatus (list<int> selectedids)  {      string result = "OK";      string errmsg = "";        return this. Jsonformat (New {result = result, ErrMsg = errmsg});  }  

Way Two

Front Code:

var _list = [];    for (var i = 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 logic< C12/>alert (' Ok ');      }  );  

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:

The custom person class public  class person  {public      string FirstName {get; set;}      public string LastName {get; set;}  }  

  

Background action public  actionresult 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 (var i = 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,      success:function (Responsejson) {          //your Logic          alert (' Ok ');      }  });  

  

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.