首页 > Web开发 > 详细

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

时间:2017-04-14 17:19:47      阅读:222      评论:0      收藏:0      [点我收藏+]
集合
后台方法:
[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集合或者实体对象的两种方法

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!