客户端代码如下:
<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://*.com/questions/23069172/posting-json-array-of-object-to-controller-in-asp-net-mvc
http://*.com/questions/2203912/asp-net-mvc-ajax-json-post-array