郑贤

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

JS 代码:

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script> 
    //ajax提交数据
    function SubmitStuAnswersAjax(starlist) { 
        $.post("index4", { stu: starlist }, function (data) {
            alert(data);
        });

    }
    //按钮点击方法
    function SubmitStuAnswersBtn() {
        var obg = new Object;
        obg.name = "黎明";
        obg.id = 1;
        obg.del = false;
        var obg2 = new Object;
        obg2.name = "黎明2";
        obg2.id = 1;
        obg2.del = true;
        var obg3 = new Object;
        obg3.name = "黎明3";
        obg3.id = 1;
        obg3.del = true;

        var star = new Array();

        star.push(obg)
        star.push(obg2)
        star.push(obg3)
        //将数组序列化json字符串
       var starlist= JSON.stringify(star)
       SubmitStuAnswersAjax(starlist);
        //console.log(stuAnswerArr)
    } 
</script> 
<input type="button" value="sofdjosdif" onclick="SubmitStuAnswersBtn() " />
View Code

 

后端代码 :

using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
   public ActionResult index4(string stu)
        { 
            var name = "";
            /// 解析JSON数组生成对象实体集合
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(stu);
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<stuEntity>));
            List<stuEntity> list = o as List<stuEntity>;
            foreach (var item in list)
            {
                 name += item.name + "_";
            }  
            return Content(name); 
        }

  public class stuEntity
    {
        public int id { set; get; }
        public string name { set; get; } 
        public bool del { set; get; } 
    }
View Code

 

posted on 2018-08-16 09:17  郑志岩  阅读(438)  评论(0编辑  收藏  举报