在项目上用到了批量删除与批量更改状态,前台使用了EasyUI的DataGrid,用到了批量更改数据状态功能。
在前台可以获取每条数据的ID,但是如何通过数组方式传递给后台?
通过昨晚的各种方式的调试,终于得出了答案! 在此作为备忘。
目前有两种方式可行:
方式一
前台代码:
-
- var_list={};
-
- for(vari=0;i<checkedRow.length;i++){
- _list["selectedIDs["+i+"]"]=checkedRow[i].ID;
- }
- $.ajax({
- url:'@Url.Action("SetCallBackStatus")',
-
- data:_list,
- dataType:"json",
- type:"POST",0); background-color:inherit">//traditional:true,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> success:function(responseJSON){
- //yourlogic
- alert('Ok');
- });
注意:
1、_list 是一个对象
2、_list中的属性需要结合后台参数名称,例如”selectedIDs“,组合成类似:selectedIDs[0],selectedIDs[1]...等Request.Params
这里是最重要的,否则后台认不出来。这种方式也可以传递自定义类的数组。组合方式就是selectedIDs[0].FirstName,selectedIDs[0].LastName,selectedIDs[1].FirstName,selectedIDs[1].LastName...
3、ajax的data参数直接指定为_list
后台代码:
publicActionResultSetCallBackStatus(List<int>selectedIDs)
- {
- stringresult="ok";
- stringerrMsg="";
- returnthis.JsonFormat(new{result=result,errMsg=errMsg});
- }
方式二
copy
var_list=[];
-
- vari=0;i<checkedRow.length;i++){
- _list[i]=checkedRow[i].ID;
- }
- $.ajax({
- url:'@Url.Action("SetCallBackStatus")',108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> data:{"selectedIDs":_list},0); background-color:inherit">//data:_list,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> dataType:"json",248)"> type:"POST",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> traditional:true,248)"> success:function(responseJSON){
- //yourlogic
- alert('Ok');
- });
1、_list 是一个数组。
2、ajax参数中data为{“selectedIDs”:_list}
3、这种方式比较重要的 traditional:true。或者将2、中的 _list参数转换一下$.param(_list,true)。这里其实就是将_list作为传统的方式传递给后台。Jquery默认是做了转换的。据说是为了使用PHP。。。。后台语言而做的。其实也就是自动在参数后面追加了”[]“。
后台代码:
同方式一
针对自定义的类,也可以通过方式一jquery ajax传递给后台
例如:
copy
//自定义Person类
- publicclassPerson
- {
- stringFirstName{get;set;}
- stringLastName{set;}
- }
//后台Action
- publicActionResultSetCallBackStatus(List<Person>selectedIDs)
- stringresult="ok";
- stringerrMsg="";
- }
此时前台js可以这样写:
copy
var_list={};
- _list["selectedIDs["+i+"].FirstName"]=checkedRow[i].FirstName;
- _list["selectedIDs["+i+"].LastName"]=checkedRow[i].LastName;
- });
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|