发布到webapi核心的Ajax始终为null

时间:2018-03-06 23:41:58

标签: c# ajax asp.net-mvc

您好我遇到了对webapi的ajax调用以及将复杂的javascript对象发布到服务器的问题。 如果我发布没有子阵列的对象一切正常。当我使用children对象发布时,发布的数据为空。

这是工作对象的示例

{"IdNews": 0, "DataNews": "2018-01-01T12:00", "Visualizza": true, "DataPubblicazione": "2018-01-01T12:00"}

这是非工作对象

{ "IdNews": 0, "DataNews": "2018-01-01T12:00", "Visualizza": true, "DataPubblicazione": "2018-01-01T12:00","NewsLingues": [{"IdLingua": 1,"Titolo": "dsfgf","Testo": "dfghdfghdfg"}]}

我检查了对象属性并且是正确的。

这是ajax电话

$.ajax({
    url: '/myapi',
    data: JSON.stringify(data),
    contentType: "application/json;charset=utf-8",
    processData: false,
    type: 'post',
    dataType: 'json',
    success: function (data) {
    },
    error: function (data) {
    }
});

并且webappi代码是

[HttpPost]
[Route("set")]
public dynamic Set([FromBody]News obj)
{
    //TODO
    return (item);
}

obj始终为null。 我尝试了各种解决方案,将contentType更改为'application / json',删除了contentType但没有一个正在运行。

这里是News对象的结构(使用EF Core 2.0使用LLBLGen创建)

public partial class News : CommonEntityBase
{
    #region Class Extensibility Methods
    /// <summary>Method called from the constructor</summary>
    partial void OnCreated();
    #endregion
    private System.Int32 _idNews = default(System.Int32);

    /// <summary>Initializes a new instance of the <see cref="News"/> class.</summary>
    public News() : base()
    {
        this.NewsLingues = new List<NewsLingue>();
        this.Slugs = new List<Slug>();
        OnCreated();
    }

    /// <summary>Gets or sets the DataInserimento field. </summary>
    public Nullable<System.DateTime> DataInserimento { get; set;}
    /// <summary>Gets or sets the DataNews field. </summary>
    public Nullable<System.DateTime> DataNews { get; set;}
    /// <summary>Gets or sets the DataPubblicazione field. </summary>
    public Nullable<System.DateTime> DataPubblicazione { get; set;}
    /// <summary>Gets the IdNews field. </summary>
    public System.Int32 IdNews => _idNews;
    /// <summary>Gets or sets the IdTenant field. </summary>
    public Nullable<System.Int32> IdTenant { get; set;}
    /// <summary>Gets or sets the Visualizza field. </summary>
    public Nullable<System.Boolean> Visualizza { get; set;}
    /// <summary>Represents the navigator which is mapped onto the association 'NewsLingue.News - News.NewsLingues (m:1)'</summary>
    public List<NewsLingue> NewsLingues { get; set;}
    /// <summary>Represents the navigator which is mapped onto the association 'Slug.News - News.Slugs (m:1)'</summary>
    public List<Slug> Slugs { get; set;}
    /// <summary>Represents the navigator which is mapped onto the association 'News.Tenant - Tenant.News (m:1)'</summary>
    public Tenant Tenant { get; set;}
}

0 个答案:

没有答案