复杂类型的Ajax模型绑定 [英] Ajax model binding of a complex type
问题描述
我正在尝试执行以下操作,其中我有一个控制器,其方法类似于:
I am trying to do something along the lines of the following where I have a Controller with an method similar to:
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提交数据,例如
From a page I want to submit data using JQuery and Ajax something like
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提交包含数组(或集合)作为属性的对象?
MVC binds the Author object (FirstName and LastName are set) but doesn't bind the Books property. Why is that and how can I submit an object containing an Array (or a Collection) as a property through AJAX?
推荐答案
DaveG,
您是否不需要使用POST方法而不是GET?
Wouldn't you need to use a POST method rather than the GET ??
即
$.ajax({
type: "POST",
url: "/Insert",
data: auth
});
我确定json格式还有其他问题,但这是我乍看之下的初衷.
i'm sure there may be other issues as well re json formatting but this is my initial thoights on 1st glance.
这篇关于复杂类型的Ajax模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!