微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Method上的Webapi参数为null

我试图将JSON格式的数据对象(WorkOrders)列表发回给我的Webapi控制器,这非常好用,除了数据对象参数(savemodel)在点击webapi时为空的轻微缺陷控制器.
这是来自JS的剪辑(插槽是模拟数据)

var slots = [];    
            slots.push({ 'WorkOrder': 'XX21', 'OrderDate': '2015-10-11 00:00:00', 'Slot': '1', 'SageRef': 'HS11' });
            slots.push({ 'WorkOrder': 'XX22', 'OrderDate': '2015-10-12 00:00:00', 'Slot': '2', 'SageRef': 'HS12' })
            slots.push({ 'WorkOrder': 'XX23', 'OrderDate': '2015-10-13 00:00:00', 'Slot': '3', 'SageRef': 'HS13' });
            console.log(JSON.stringify({ 'savemodel': slots }))
            $.ajax({
                type: "POST",
                url: 'http://localhost:55821/api/schedule',
                data: JSON.stringify({ 'savemodel': slots }),
                contentType: 'application/json; charset=utf-8'                
            }).success(function (data) {
                $scope.$apply(function () {
                    if (data.SaveMessage.length > 0) {
                      // do something
                    }
                    else {
                      // do something else
                    }
                });
            });

该模型:

    public class WorkOrderModel
{
    public string WorkOrder { get; set; }
    public string OrderDate { get; set; }
    public string SlotNumber { get; set; }
    public string SageRef { get; set; }
}

回发方法

 [HttpPost]
        public IHttpActionResult UpdateWorkOrder([FromBody]List<WorkOrderModel> savemodel)
        {
            StringBuilder saveMessage = new StringBuilder();
            foreach (WorkOrderModel model in savemodel)
            {
                   // save the WO model object  here                
            }
            if (saveMessage.Length > 0)
            {
                return Ok(new { SaveMessage = "There were issues: " + saveMessage.ToString() });
            }
            else
            {
                return Ok(new { SaveMessage = "" });
            }
        }

Posted JSON
Null parameter in the WebApi Controller
这让我疯了,所以在这个阶段,任何帮助都将受到极大的赞赏!

问候,
itsdanny

解决方法:

排序,它改变了

data:JSON.stringify({‘savemodel’:slots}),

data:JSON.stringify(slots),

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐