我在ASP.NET MVC中使用骨干。我想将模型保存到服务器。
Folder = Backbone.RelationalModel.extend({
url: "/SaveFolder",
relations: [
{
type: Backbone.HasMany,
key: 'Files', /*key should match the JSON name*/
relatedModel: 'FileModel',
collectionType: 'FileCollection',
}
],
idAttribute: "FolderID"
});
var newFolder = new Folder();
newFolder.set("FolderName", newFolderName);
newFolder.set("EditableByOthers", "N");
newFolder.save({
success: function(model, response, options) {
alert("success"); //not reached
},
error: function (model, xhr, options) {
alert("error"); //not reached
}
});
{"Files":[],"FolderName":"new","EditableByOthers":"Y"}
[HttpPost]
public ActionResult SaveFolder(Folder newFolder) //All properties of newFolder are null, or "N" in case of boolean
{
string name = newFolder.FolderName;
return null;
}
public class Folder
{
public int? FolderId { get; set; }
public int UserId;
public string FolderName;
public bool EditableByOthers;
public IList<File> Files;
}
我无法真正指出这里有什么可疑的东西。但同样,我是一个骨干新手,所以我很容易错过一些东西。任何人都可以看到什么是错的吗?
答案 0 :(得分:1)
主要问题是在模型上使用公共字段而不是属性。简单的问题,不是那么简单的定位。这篇文章帮助了我 - How to pass complex type using json to ASP.NET MVC controller