zwei1121

博客园 首页 新随笔 联系 订阅 管理

http://blog.csdn.net/lonestar555/article/details/10192595/

 

 

  1. // 方式一  
  2. var _list = {};  
  3.   
  4. for (var i = 0; i < checkedRow.length; i++) {  
  5.     _list["selectedIDs[" + i + "]"] = checkedRow[i].ID;  
  6. }  
  7.   
  8. $.ajax({  
  9.     url: '@Url.Action("SetCallBackStatus")',  
  10.     //data: { "selectedIDs": _list },  
  11.     data: _list,  
  12.     dataType: "json",  
  13.     type: "POST",  
  14.     //traditional: true,  
  15.     success: function (responseJSON) {  
  16.         // your logic  
  17.         alert('Ok');  
  18.     }  
  19. });  

在action中的参数以数组方式接收数据

 一、表单方式

1、提交Form

[html] view plain copy
 
  1. <form action="../../test/test" method="post">  
  2.   
  3. <select name="list_Number" id="list_Number" size="10"  multiple="multiple">  
  4. <option value="4">04</option>  
  5. <option value="2">02</option>  
  6. <option value="3">03</option>  
  7. </select>   
  8.   
  9. </form>  


 

[csharp] view plain copy
 
  1. [HttpPost]  
  2.         public ActionResult Test(string[] listNumber){  
  3.   
  4. return View();  
  5. }  


提交前必须选择所有列表:

[javascript] view plain copy
 
  1. $("#list_LotteryNumber > option").attr("selected", true);  


 2、ajax提交

在ajax的data参数中加上:

 

[javascript] view plain copy
 
  1. $("form").serialize()  

 

[javascript] view plain copy
 
  1. data: $("form").serialize(),  



 

二、ajax提交数组参数

 

 

 

[javascript] view plain copy
 
  1.      var parm = {  
  2.                 capital: "capital",  
  3.                 arr: [  "c1","c2", "c3"],  
  4.                 data: Math.random()  
  5.             };   
  6.             var p2 = $.param(parm, true);   
  7. //   或         var parm3 = "arr=c1&arr=c2&arr=c3&data=" + Math.random();    
  8.   
  9. //            debugger  
  10.             $.ajax({  
  11.                 type: "post",  
  12.                 url: '/Home/AjaxArray',  
  13.                 async: false,  
  14.                 data: p2,  
  15.                 success: function (data, txtStatus) {  
  16.                     alert(" 成功!");  
  17.                 }  
  18.             });  



 

 

[csharp] view plain copy
 
    1. [HttpPost]  
    2.         public ActionResult AjaxArray(string data , List<string> arr)  
    3.         {  
    4.             ViewBag.Message = "提交数组参数。";  
    5.   
    6.             return Json("");  
    7.         }  
posted on 2016-06-17 14:51  zwei  阅读(1238)  评论(0编辑  收藏  举报