将Json数组绑定到模型Web Api

将Json数组绑定到模型Web Api,json,entity-framework,asp.net-web-api,model-binding,Json,Entity Framework,Asp.net Web Api,Model Binding,我有这样的类结构。问题是,当我传输到之前序列化的web api控制器用户json时,actionees计数列表为0。 序列化Json如下所示: {"$id":"1","UserId":1,"RealName":"vas`ka","PasswordHash":"password", "Email":"somemail","Actionees": {"$id":"2","$values": [{"$id":"3","ActioneeId":1,"Status":"doing",

我有这样的类结构。问题是,当我传输到之前序列化的web api控制器用户json时,actionees计数列表为0。 序列化Json如下所示:

{"$id":"1","UserId":1,"RealName":"vas`ka","PasswordHash":"password",
 "Email":"somemail","Actionees":
   {"$id":"2","$values": 
      [{"$id":"3","ActioneeId":1,"Status":"doing", "CloseDate":null,
        "DeletedFlag":false, "CreationDate":"2013-03-11T11:48:24.08", 
        "Users":null,  "Comments":null}, {"$id":"4","ActioneeId":2,"Status":"doing",  
        "CloseDate":null, "DeletedFlag":false, 
        "CreationDate":"2013-03-11T12:46:08.787", "Users":null,"Comments":null}
      ]
   }
}


public class User
{
    [Key]
    public Int64 UserId { get; set; }
     public String Email { get; set; }
    public String RealName { get; set; }
    public String PasswordHash { get; set; }

    public virtual List<Actionee> Actionees { get; set; }
}

public class Actionee
{
    [Key]
    public Int64 ActioneeId { get; set; }
    public String Status { get; set; }
    public DateTime? CloseDate { get; set; }
    public Boolean DeletedFlag { get; set; }
    public DateTime? CreationDate { get; set; }

    public virtual List<User> Users { get; set; }
    public virtual IEnumerable<Actionee> Comments { get; set; } 
}
        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false,  true, null);
        xml.SetSerializer<User>(dcs);

        var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.All;   

       And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
{“$id”:“1”,“UserId”:1,“RealName”:“vas`ka”,“PasswordHash”:“password”,
“电子邮件”:“somemail”,“Actionees”:
{“$id”:“2”,“$values”:
[{“$id”:“3”,“ActioneeId”:1,“Status”:“doing”,“CloseDate”:null,
“DeletedFlag”:false,“CreationDate”:“2013-03-11T11:48:24.08”,
“Users”:null,“Comments”:null},{“$id”:“4”,“ActioneeId”:2,“Status”:“doing”,
“CloseDate”:null,“DeletedFlag”:false,
“CreationDate”:“2013-03-11T12:46:08.787”,“用户”:null,“评论”:null}
]
}
}
公共类用户
{
[关键]
公共Int64用户标识{get;set;}
公共字符串电子邮件{get;set;}
公共字符串实名{get;set;}
公共字符串密码哈希{get;set;}
公共虚拟列表操作对象{get;set;}
}
公开诉讼对象
{
[关键]
公共Int64 ActioneeId{get;set;}
公共字符串状态{get;set;}
公共日期时间?CloseDate{get;set;}
公共布尔DeletedFlag{get;set;}
公共日期时间?CreationDate{get;set;}
公共虚拟列表用户{get;set;}
公共虚拟IEnumerable注释{get;set;}
}
我在应用程序_start中添加了以下字符串:

        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false,  true, null);
        xml.SetSerializer<User>(dcs);

        var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.All;   

       And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
var xml=GlobalConfiguration.Configuration.Formatters.XmlFormatter;
var dcs=新的DataContractSerializer(typeof(User),null,int.MaxValue,false,true,null);
xml.SetSerializer(dcs);
var json=GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling=
Newtonsoft.Json.PreserveReferencesHandling.All;
我试图以这种方式反序列化:http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization

由于实体框架对象序列化,您的json格式不正确

        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false,  true, null);
        xml.SetSerializer<User>(dcs);

        var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.All;   

       And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
你有额外的属性,比如

        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false,  true, null);
        xml.SetSerializer<User>(dcs);

        var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.All;   

       And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
$id:“2”,“$values”

        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false,  true, null);
        xml.SetSerializer<User>(dcs);

        var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.All;   

       And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
JsonFormatter无法识别额外属性,也无法对对象进行反序列化 您需要将实体框架对象转换为用户对象
在web api控制器中,由于实体框架对象序列化,json格式不正确

        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false,  true, null);
        xml.SetSerializer<User>(dcs);

        var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.All;   

       And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
你有额外的属性,比如

        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false,  true, null);
        xml.SetSerializer<User>(dcs);

        var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.All;   

       And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
$id:“2”,“$values”

        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false,  true, null);
        xml.SetSerializer<User>(dcs);

        var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.All;   

       And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
JsonFormatter无法识别额外属性,也无法对对象进行反序列化 您需要将实体框架对象转换为用户对象

在您的web api控制器中

您正在使用json.net?没有。我曾尝试在WebApi中使用newtonsoft进行此操作,但它可以序列化,但当我尝试绑定时,没有ActionEE列表。您可以添加一点序列化和反序列化代码吗?请通过添加此代码编辑您的问题吗(序列化和反序列化方法)我可以测试它最后一个问题:字符串结果是实体框架对象序列化的结果?您使用的是json.net?否。我曾尝试在WebApi中使用newtonsoft进行此操作,但它可以序列化,但当我尝试绑定时,没有ActionEE列表。请添加一点序列化和反序列化代码好吗通过添加此代码(序列化和反序列化方法)编辑您的问题我可以测试它最后一个问题:字符串结果是实体框架对象序列化的结果?这意味着我必须使用未命名实体框架实体的模型,并且我的模型可能具有相同的结构?是的,另一种选择是使用“代码优先”实体框架的方法当我将数据复制到模型时,它不会解决我的结果json的格式。我用媒体类型格式化程序解决这个问题。就像在msdn示例中一样。谢谢你的帮助。但我关心的是总是将数据从实体复制到模型。这是一种正确的方法吗?试着用实体框架对象反序列化它这意味着我必须使用未命名实体框架实体的模型,并且我的模型可能具有相同的结构?是的,另一种选择是使用“代码优先”实体框架的方法当我将数据复制到模型时,它不会解决我的结果json的格式。我用媒体类型格式化程序解决这个问题。就像在msdn示例中一样。谢谢你的帮助。但我关心的是总是将数据从实体复制到模型。这是一种正确的方法吗?试着用实体框架对象反序列化它您的用户对象的
        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false,  true, null);
        xml.SetSerializer<User>(dcs);

        var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.All;   

       And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization