无法将可验证的对象从$ http发布到web api

时间:2017-01-11 12:22:20

标签: c# angularjs ajax serialization asp.net-web-api

web api有$("#tblJQGridCCVT").jqGrid({ url: "@Url.Action("MyAction", "MyController")" + "?Parameters=" + Params + "", datatype: "json", mtype: 'GET', cache: false, async: false, colNames: ['A', 'B', 'C', 'D', 'E','F', so on...],//nearly 30 columns colModel: [ { name: 'A', index: 'A', width: 150, edittype: 'select', formatter: linkFmatter }, { name: 'B', index: 'B', width: 150 }, { name: 'C', index: 'C', width: 150 }, { name: 'D', index: 'Updated By', width: 150 }, { name: 'E', index: 'E', width: 150 }, { name: 'F', index: 'F', width: 150 }, So on ... ... ... ], pager: $('#pager'), height:300, rowNum: 10, sortorder: "desc", sortname: 'ResponseId', viewrecords: true, sortable: true, loadonce: true, forceClientSorting: true, ignoreCase: true, caption: "Summary" }); $("#tblJQGridCCVT").jqGrid('navGrid', '#pager', { view: false, del: false, add: false, edit: false, search: true, refreshtext: "Refresh" }, { closeOnEscape: true, multipleSearch: true, closeAfterSearch: true }, {}, {}, {}); $("#tblJQGridCCVT").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: 'cn' }); function linkFmatter(cellvalue, options, rowObject) { var selectedCellValue = cellvalue; var selectedRowId = options.rowId; return '<a href="javascript:MethodJS(' + selectedRowId + ')" style="color: #3366ff" id="' + selectedRowId + '" >' + selectedCellValue + '</a>'; } function MethodJS(selectedRowId) { $.ajax({ async: false, type: "POST", contentType: "application/json; charset=utf-8", url: "@Url.Action("GetDetailedViewOfResponsesForEdit", "ViewResponseDetailsCCVT")", data: "{RowId:'" + selectedRowId + "'}", success: function (Result) { if (Result == "Success") { var url = window.location.href; window.location.href = "@Url.Action("ResponseDetailsEditForCCVT", "ViewResponseDetailsCCVT")"; } } }); } 个模型对象,因为需要强烈的血清化。 现在,当我使用下面的代码从[Serilizable]调用发布该模型对象时,它发布时没有错误,但是没有数据填充,所有值都在web api上发布。 如果我删除$.http属性,那么工作正常并能够获得所有发布的值。

[Serilizable]

web api POST方法将参数作为以下对象:

return $http({
                method: "POST",
                url: config.APIURL + 'Parts',
                data: JSON.stringify(part), // Strinify your object
                headers: { 'Content-Type': 'application/json' }
            });

请在这里帮助我。

1 个答案:

答案 0 :(得分:0)

最终找到解决方案。需要使用[JsonObject][JsonProperty]属性标记对象,如下所示。

 [Serializable]
    [JsonObject]
    public class SNPart
    {
        [JsonProperty]
        public double? PartLength { get; set; }
        [JsonProperty]

感谢 dhaval