在前台提交(post)的数据中。除了强类型的数据外,还有一个额外的json数据提交

在这里我的办法是,在前台把json对象转换成字符串,然后提交。

测试demo

前台:

    @using(Html.BeginForm())
    {
        <input type="text" />
        <input type="submit" value="提交"/>
    }

<script>
    var json = [{ "Name": "小马宝莉", "ID": 9, "Stock": "abc" }];
    json.push({ "Name": "海绵宝宝", "ID": 8, "Stock": "xyz" })
    var jsonstr = JSON.stringify(json);

    $('#json').val(jsonstr);
    //alert(jsonstr);
</script>

  后台:

       [HttpPost]
        public ActionResult AjaxPager(string json)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            //JsonClass jsonClass = (JsonClass)js.Deserialize(json, typeof(JsonClass)); ;
            //var test = js.Deserialize(json, typeof(JsonClass));
            
            // 如果是一维数组的json用这个
            // JsonClass jc = js.Deserialize<JsonClass>(json);


            // 把多维数组转换成List泛型。
            List<JsonClass> jc = js.Deserialize<List<JsonClass>>(json);

            return View();
        }
    }
    public class JsonClass
    {
        public string Name { get; set; }
        public int ID { get; set; }
        public string Stock { get; set; }
    }

  这样就可以方便处理了。

记录一下。备用

相关文章:

  • 2021-11-22
  • 2021-11-11
  • 2021-11-21
  • 2021-08-02
  • 2022-12-23
  • 2021-12-10
  • 2021-12-31
  • 2021-09-25
猜你喜欢
  • 2021-11-21
  • 2021-06-04
  • 2018-03-03
  • 2021-09-08
  • 2021-10-11
  • 2022-12-23
相关资源
相似解决方案