通过AJAX将保存实体+嵌套列表保存到Web API

时间:2014-08-26 07:20:22

标签: c# asp.net asp.net-web-api

我目前正在使用WebAPI 2控制器进行ajax调用的Web表单(而不是MVC)应用程序。

我正在尝试创建/更新一个对象,该对象还包含另一个实体的列表:

这是实体对象的精简版:。

public partial class POHdr
{
    public int POHdrID { get; set; }

...snip...

    public POHdr()
    {
        this.POLine = new List<POLine>();
    }

    public virtual List<POLine> POLine { get; set; }
}

ajax调用是这样的:

$.ajax({
    url: ajaxURL,
    type: "PUT",
    dataType: 'json',
    data: jOBJ,
    contentType: 'application/json',
    success: function (msg) {
        AJAXSuccess(msg);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        AJAXError(XMLHttpRequest, textStatus, errorThrown);
    }
});

'jOBJ'(这是JSON.stringify的结果)如下所示:

"{"ExchRate":"","DelAddr1":"","DelAddr2":"","DelAddr3":"","DelAddr4":"","DelAddr5":"","OrderDate":"","ReqdDate":"","Phone":"234234","Mobile":"342","Email":"Test","Fax":"","Contact":"","POLine":"[{\"Estimate\":\"123\",\"StockDesc\":\"namedesc\",\"LineSort\":0}]"}"

控制器只是一个带有实体动作控制器的普通webapi2,接收/保存POHdr对象没问题,但是当它进入控制器时,POLine总是空的。

我已经看过并且只找到了与MVC相关的解决方案,或者需要2个单独的调用来保存,理想情况下我想将其保存为一个。这可能吗?

编辑:根据要求,这里是POLine的代码,我想补充一点,[DataContract] / [DataMember]也在POHdr中,为了便于阅读,我将其删除了。

[DataContract]
public partial class POLine
{
    [DataMember]
    public int POLineID { get; set; }
    [DataMember]
    public Nullable<int> CompanyID { get; set; }
    [DataMember]
    public Nullable<int> POHdrID { get; set; }
    [DataMember]
    public Nullable<int> LineSort { get; set; }
    [DataMember]
    public Nullable<int> StockID { get; set; }
    [DataMember]
    public string Stockcode { get; set; }
    [DataMember]
    public string StockDesc { get; set; }
    [DataMember]
    public Nullable<int> StkUOMID { get; set; }
    [DataMember]
    public Nullable<double> OrderQty { get; set; }
    [DataMember]
    public Nullable<double> UnitPrice { get; set; }
    [DataMember]
    public Nullable<bool> Estimate { get; set; }
    [DataMember]
    public Nullable<double> Discount { get; set; }
    [DataMember]
    public Nullable<double> LineValue { get; set; }
    [DataMember]
    public Nullable<double> GST { get; set; }
    [DataMember]
    public Nullable<System.DateTime> CreateDate { get; set; }
    [DataMember]
    public Nullable<System.DateTime> LastUpdated { get; set; }
    [DataMember]
    public Nullable<System.DateTime> ReqdDate { get; set; }
    [DataMember]
    public virtual Stock Stock { get; set; }
    [DataMember]
    public virtual POHdr POHdr { get; set; }
}

编辑2:控制器的相关代码

[ResponseType(typeof(void))]
public IHttpActionResult PutItem(int id, POHdr item)
{
    try
    {
        Actions.UpdateItem(id, item);
    }
    catch (Exception ex)
    {
        return StatusCode(HttpStatusCode.NotFound);
    }
    return StatusCode(HttpStatusCode.OK);
}

// POST: api/PurchaseOrder
[ResponseType(typeof(POHdr))]
public IHttpActionResult PutItem(POHdr item)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    Actions.AddItem(item);
    return CreatedAtRoute("DefaultApi", new { id = item.POHdrID }, item);
}

1 个答案:

答案 0 :(得分:0)

问题是JSON字符串无效(我没有通过验证器运行它来检查)

数组编码的方式导致'POLine'的数组只是一个垃圾字符串而不是实际的json(这就是字符串中有反斜杠的原因)。