我正在尝试执行以下操作,其中我有一个 Controller ,其方法类似于:
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 绑定(bind) Author 对象(设置了 FirstName 和 LastName),但不绑定(bind) Books 属性。这是为什么?如何通过 AJAX 提交包含数组(或集合)作为属性的对象?
最佳答案
戴夫G,
您不需要使用 POST 方法而不是 GET 吗?
即
$.ajax({
type: "POST",
url: "/Insert",
data: auth
});
我确信可能还有其他问题以及重新格式化 json,但这是我乍一看的最初想法。
关于jquery - 复杂类型的 Ajax 模型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2092616/