在后Web.Api不反序列化儿童阵列的属性 [英] Web.Api not deserializing properties of Child Array in Post

查看:91
本文介绍了在后Web.Api不反序列化儿童阵列的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正确地从JSON在浏览器中反序列化一个复杂的对象,但是当我张贴同样的数据备份与jQuery,数组的子对象没有得到反序列化 - 它们都被设置为默认值<。 / p>

我试着从列表改变集合类型,以IEnumerables,但是这并没有改变。有趣的是,我回来元件的数目是正确的金额。

另外这是一个CORS职位,但我已经没有任何问题,否则。

模式

 公共类文档
{
    公众诠释DocumentId {搞定;组; }
    公共DocumentType DocumentType {搞定;组; }
    公共字符串名称{;组; }
    公共字符串路径{搞定;组; }
    公共IEnumerable的&LT; D​​ocumentField&GT; {领域获得;组; }
}公共类DocumentField
{
    公众诠释FieldId {搞定;组; }
    公共字符串路径{搞定;组; }
    公共IEnumerable的&LT; FieldRelationship&GT; RelatedFields {搞定;组; }
}公共类FieldRelationship
{
    公众诠释RelationshipId {搞定;组; }
    公众诠释FieldId {搞定;组; }
}

下面是我的控制器方法签名(注意我没有 [FromBody] 属性的参数,当我做我得到一个空的结果)。

  [HttpPost]
公共无效后(Web.Document webDoc)

jQuery的

  VAR解析度= $获得(的http://本地主机:11786 / API /文件/ 1');
res.then(功能(数据){
  $阿贾克斯({
     输入:POST,
     网址:的http://本地主机:11786 / API /文件/',
     跨域:真实,
     数据:数据,
     成功:函数(responseData,textStatus,jqXHR){
       的console.log('是');
     },
     错误:功能(responseData,textStatus,errorThrown){
       的console.log('失败');
     }
   });
 });

响应体是什么样子:

 字段[0] [FieldId] = 1410
&放大器;场[0] [路径] = TestField
&放大器;字段[0] [RelatedFields] [0] [RelationshipId] = 1
&放大器;字段[0] [RelatedFields] [0] [FieldId] = 503
&放大器;字段[0] [RelatedFields] [1] [FieldId] = 501
&安培;领域[1] [路径] =饼干
&安培; DocumentId = 1
&安培; DocumentType = 2
&放大器;名称=测试文档
&放大器;路径= test.docx

的值是在所得到的C#文件实例什么

  DocumentId:1
DocumentType:2
产品名称:测试文档
路径:test.docx
字段:[{
  FieldId:0,&LT; ---坏
  路径:空,&LT; ---坏
  RelatedFields:空,&LT; ---坏
},{
  FieldId:0,&LT; ---坏
  路径:空,&LT; ---坏
  RelatedFields:空
}]

修改

下面是结果,如果我改变我如何在Ajax调用发送的数据。记得数据是毕业于得到的结果变量。

  ajax.data [FromBody]结果
--------- ---------- ------
数据假道具设置,阵算正确,数组对象道具空
JSON.stringfy(数据)虚假对象不为空,但空道具
{:数据}虚假对象不为空,但空道具
=+ JSON.stringfy虚假对象不为空,但空道具
数据真正的零
JSON.stringfy(数据)真正的零
{:数据}真正的零
=+ JSON.stringfy真空

如果我用角的$ http.post( $ http.post(的http://本地主机:11786 / API /文件/',res.data); ),并添加 [FromBody] 属性的方法的参数,我得到正确的结果。好像在某种程度上就是我传递给数据$就不是正确的事情,即使我尝试了上述四种方法。


解决方案

添加的contentType 来你的Ajax设置对象,JSON.stringify您的文档对象,张贴JSON字符串到服务器

VAR DOC = {DocumentId:1};
VAR jsonDoc = JSON.stringify(DOC);
jQuery.ajax(API /测试',{
    输入:POST,
    数据:jsonDoc,
    的contentType:应用/ JSON
})
.done(功能(数据,状态,JQR){
    警报(状态);
})

这应该工作。

I have a complex object that is correctly deserialized from json in the browser, but when I post the same data back with jQuery, the child objects of an array aren't getting deserialized - they are all set to default values.

I've tried changing the collections types from List, to IEnumerables, but that has changed nothing. It is interesting that the number of elements I get back is the correct amount.

Also this is a CORS POST, but I haven't had any problems otherwise.

Models

public class Document 
{
    public int DocumentId { get; set; }
    public DocumentType DocumentType { get; set; }
    public string Name { get; set; }
    public string Path { get; set; }
    public IEnumerable<DocumentField> Fields { get; set; }
}

public class DocumentField
{
    public int FieldId { get; set; }
    public string Path { get; set; }
    public IEnumerable<FieldRelationship> RelatedFields { get; set; }
}

public class FieldRelationship
{
    public int RelationshipId { get; set; }
    public int FieldId { get; set; }
}

Here is my controller method signature (notice I don't have [FromBody] attribute on the parameter, when I did I got a null result).

[HttpPost]
public void Post(Web.Document webDoc)

jQuery

var res = $.get('http://localhost:11786/api/documents/1');
res.then(function (data) {   
  $.ajax({
     type: 'POST',
     url: 'http://localhost:11786/api/documents/',
     crossDomain: true,
     data: data,
     success: function (responseData, textStatus, jqXHR) {
       console.log('yes');
     },
     error: function (responseData, textStatus, errorThrown) {
       console.log('failed');
     }
   });
 });

What the Response Body looks like:

Fields[0][FieldId]=1410
&Fields[0][Path]=TestField
&Fields[0][RelatedFields][0][RelationshipId]=1
&Fields[0][RelatedFields][0][FieldId]=503
&Fields[0][RelatedFields][1][FieldId]=501
&Fields[1][Path]=cookies
&DocumentId=1
&DocumentType=2
&Name=Test Document
&Path=test.docx 

What the values are in the resulting C# Documents instance

DocumentId: 1
DocumentType: 2
Name: "Test Document"
Path: "test.docx"
Fields: [{
  FieldId: 0,             <--- Bad
  Path: null,             <--- Bad
  RelatedFields: null,    <--- Bad
}, {
  FieldId: 0,             <--- Bad
  Path: null,             <--- Bad
  RelatedFields: null
}]

Edit

Here are the results if I change how I send the data in the ajax call. Remember data is the variable that holds the result from get.

ajax.data         [FromBody]  Result
---------         ----------  ------
data                   false  Props set, array count correct, array object props null
JSON.stringfy(data)    false  Object not null, but props null
{ "": data }           false  Object not null, but props null
"=" + JSON.stringfy    false  Object not null, but props null
data                    true  null
JSON.stringfy(data)     true  null
{ "": data }            true  null
"=" + JSON.stringfy     true  null

If I use angular's $http.post ($http.post('http://localhost:11786/api/documents/', res.data);) and add the [FromBody] attribute to the method parameter, I get the correct results. It seems like somehow what I'm passing to data in $.ajax is not the right thing, even though I tried the four methods above.

解决方案

Add contentType to your ajax setting object, JSON.stringify your doc object, post json string to server:

var doc = { DocumentId: 1 };
var jsonDoc = JSON.stringify(doc);
jQuery.ajax('api/test', {
    type: 'POST',
    data: jsonDoc,
    contentType: 'application/json'
})
.done(function(data, status, jqr) {
    alert(status);
})

It should works.

这篇关于在后Web.Api不反序列化儿童阵列的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆