Ajax模型绑定的复杂类型

我试图按照以下方式做一些事情,我有一个控制器,其方法类似于:

public ActionResult Insert(Author author) { //do something... } 

作者类型的位置如下:

 public class Author { public string FirstName { get; set; } public string LastName { get; set; } public Book[] Books { get; set; } public Author() { Books = new Book[0]; } } public class Book { public string Title { get; set; } public int NumberOfPages { get; set; } } 

从一个页面我想使用JQuery和Ajax提交数据

 function addAuthor() { var auth = { 'FirstName': 'Roald', 'LastName': 'Dahl', 'Books': [ { 'Title': 'Charlie and the Chocolate Factory', 'NumberOfPages': 264 }, { 'Title': 'The Twits', 'NumberOfPages': 316 } ] }; $.ajax({ type: "GET", url: "/Insert", data: auth }); } 

MVC绑定Author对象(设置FirstName和LastName)但不绑定Books属性。 为什么这样,我如何通过AJAX提交包含数组(或集合)作为属性的对象?

DaveG,

你不需要使用POST方法而不是GET吗?

  $.ajax({ type: "POST", url: "/Insert", data: auth }); 

我确定可能还有其他问题以及re json格式化,但这是我第一眼看到的初始洞察力。

这为我修复了它: https : //stackoverflow.com/a/9775470/647845

它归结为添加

 contentType: 'application/json' 

并在发送数据之前添加JSON.stringify:

 data: JSON.stringify(dataObj),