码迷,mamicode.com
首页 > Web开发 > 详细

MVC中post集合或者实体对象的两种方法

时间:2017-04-14 17:18:37      阅读:667      评论:0      收藏:0      [点我收藏+]

标签:test   pos   lis   text   ati   isnull   ret   data   ica   

集合
后台方法:
[HttpPost]
public bool SaveData(List<SelectListItem> list)
{
      return list != null && list.Count > 0;
}
 
如果传入的参数为 
var tt = [
    { Selected: true, Text: "test1", Value: "1" },
    { Selected: false, Text: "test2", Value: "1" },
    { Selected: false, Text: "test3", Value: "1" }
];
 
方法一:(成功)
$.post("http://localhost:9011/Home/SaveData2", { list : tt }, function (arr) {
    alert(arr);  //结果为false
});
 
方法二:(成功)
$.ajax({
   type: "post",
   url: "http://localhost:9011/Home/SaveData2",
   contentType: "application/json",
   data: JSON.stringify(tt),
   success: function (arr) {
      alert(arr); //结果为true
   }
});
 
单对象
后台方法:
[HttpPost]
public bool SaveData2(SelectListItem item)
{
    return item != null && string.IsNullOrEmpty(item.Text) == false;
}
 
如果传入的参数为 
var tt = { Selected: true, Text: "test1", Value: "1" };
 
方法一:(成功)
$.post("http://localhost:9011/Home/SaveData2", tt, function (arr) {
    alert(arr);  //结果为true
});
 
方法二:(成功)
$.ajax({
   type: "post",
   url: "http://localhost:9011/Home/SaveData2",
   contentType: "application/json",
   data: JSON.stringify(tt),
   success: function (arr) {
      alert(arr); //结果为true
   }
});

MVC中post集合或者实体对象的两种方法

标签:test   pos   lis   text   ati   isnull   ret   data   ica   

原文地址:http://www.cnblogs.com/hogan/p/6709619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!