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

asp.net mvc 绑定客户端post过来的复杂JSON数据

时间:2016-06-21 15:17:08      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

客户端代码如下:

<input type="button" id="btnTest" value="测试" /><br /><br />
<textarea id="txtArea" rows="5" cols="200"></textarea>

<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#btnTest").click(function () {
            var users = new Array();
            var user = { UserName: "subendong", Password: "123456" };
            users.push(user);
            user = { UserName: "bendongsu", Password: "654321" };
            users.push(user);
            var userData = {Users : users, Address: "苏州市"};

            $.ajax({
                type: post,
                url: "@Url.Action("Test")",
                data: JSON.stringify(userData),
                dataType: "json",
                contentType: application/json,
                success: function (data) {
                    $("#txtArea").val(JSON.stringify(data));
                }
            });
        });
    });
</script>

红色部分是关键

后端代码如下:

        public ActionResult Test(UserData userData)
        {
            return Json(userData);
        }

UserData类型:

    public class UserData
    {
        public List<User> Users { get; set; }
        public string Address { get; set; }
    }

    public class User
    {
        public string UserName { get; set; }
        public string Password { get; set; }
    }

 

参考网址:

http://stackoverflow.com/questions/23069172/posting-json-array-of-object-to-controller-in-asp-net-mvc

http://stackoverflow.com/questions/2203912/asp-net-mvc-ajax-json-post-array

asp.net mvc 绑定客户端post过来的复杂JSON数据

标签:

原文地址:http://www.cnblogs.com/subendong/p/5603426.html

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