骨干保存,服务器上未收到模型数据

时间:2013-08-23 19:47:33

标签: asp.net-mvc json post backbone.js controller

我在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
        }
    });

注意:成功和错误警报永远不会触发。

当我签入Fiddler时,发送的JSON是 -

{"Files":[],"FolderName":"new","EditableByOthers":"Y"}

然而,我在serveride上的控制器收到一个空文件夹对象 -

    [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;
}

我无法真正指出这里有什么可疑的东西。但同样,我是一个骨干新手,所以我很容易错过一些东西。任何人都可以看到什么是错的吗?

1 个答案:

答案 0 :(得分:1)

主要问题是在模型上使用公共字段而不是属性。简单的问题,不是那么简单的定位。这篇文章帮助了我 - How to pass complex type using json to ASP.NET MVC controller