如何将JS对象的集合发送到ASP.NET API服务?

巴沙尔·阿布·莎玛(Bashar Abu Shamaa)

我正在尝试将JavaScript对象的集合发送到我的API服务,但是服务器接收到空的对象列表!

<script>

    //Collection
    var Collection = function () {
        this.count = 0;
        this.collection = {};

        this.add = function (key, item) {
            if (this.collection[key] != undefined)
                return undefined;
            this.collection[key] = item;
            return ++this.count
        }
        this.remove = function (key) {
            if (this.collection[key] == undefined)
                return undefined;
            delete this.collection[key]
            return --this.count
        }
        this.item = function (key) {
            return this.collection[key];
        }
        this.forEach = function (block) {
            for (key in this.collection) {
                if (this.collection.hasOwnProperty(key)) {
                    block(this.collection[key]);
                }
            }
        }
    }

// the JavaScript class for food 
  function foodcls(no,qunt,name, cals, carb, fat, prt,unt) {
        this.no = no;
        this.qunt = qunt;
        this.name = name;
        this.cals = cals;
        this.carb = carb;
        this.fat = fat;
        this.prt = prt;
        this.unt = unt;
    }
// instantiate new obj
var fod = new foodcls(3, 5, 'SomeName', 300, 180, 100, 20, 'Gram');
var fno =333;


var timCol = new Collection();
 timCol.add(fno, fod); 

  var urlpaths = '/api/FoodServ';
 $.ajax({
        url: urlpaths, 
        method: "POST",
        contentType: 'application/json;  charset=utf-8',
        data: JSON.stringify(timCol),
        success: function (data) {
// any thing
}
});

</script>

ASP.NET API中的代码:

      [HttpPost]
    public HttpResponseMessage Post(List<foodcls> fods) // Here fods is empty
    {
        int rslt=0;
        string UserId = "[email protected]";//User.Identity.Name;
        List<Foods_Meal_TBL> fooditems = new List<Foods_Meal_TBL>();
          if (fods.Count()>0)
          {
              foreach (foodcls item in fods)
        {
            Foods_Meal_TBL fooditem = new Foods_Meal_TBL();
            fooditem.FoodNo = item.no;
            fooditem.Quantity = item.qunt;
            fooditem.PersonID = UserId;
fooditems.Add(fooditem);
        }
         }
        rslt = SaveAllItems(fooditems); // rslt Meal No
        return Request.CreateResponse(HttpStatusCode.OK, rslt);
    }

有什么帮助吗?

巴沙尔·阿布·莎玛(Bashar Abu Shamaa)

我找到了通过将集合转换为数组来解决问题的方法,我知道这不是最好的解决方案,并且我希望有人能够找到避免使用数组的更好的解决方案,但是在有人给出更好的答案之前,我会尽力解决如下所示:我已经将函数添加到了Arr // //隐式收集到数组,如下所示:

//Collection
var Collection = function () {
this.count = 0;
this.collection = {};

this.add = function (key, item) {
    if (this.collection[key] != undefined)
        return undefined;
    this.collection[key] = item;
    return ++this.count
}
this.remove = function (key) {
    if (this.collection[key] == undefined)
        return undefined;
    delete this.collection[key]
    return --this.count
}
this.item = function (key) {
    return this.collection[key];
}
this.forEach = function (block) {
    for (key in this.collection) {
        if (this.collection.hasOwnProperty(key)) {
            block(this.collection[key]);
        }
    }
}
this.toArr = function (block) {   //ToArray
    var fodarr = [];
    for (key in this.collection) {
        if (this.collection.hasOwnProperty(key)) {
            fodarr.push(this.collection[key]);// block(this.collection[key]);
        }
    }
    return fodarr;
}

}

ASP.NET API函数:

  [HttpPost]
        public HttpResponseMessage Post(foodcls[] fods)

希望以后能对某人有所帮助...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将2个对象发送到我的API(ASP.NET)?

如何将GET请求从Internet Explorer发送到Asp.Net Core API?

ASP.NET表单-如何将表单数据发送到外部WCF服务

如何将多部分/表单数据发送到ASP.NET Core Web API?

如何将表单的数据发送到用Angular中的[FromForm]装饰的ASP.NET Core Web API操作?

ASP.NET Web API将复杂对象发送到前端

如何将带有 FormData 的地图对象发送到 .NET API?

如何将文件发送到列表-.NET Core Web API-邮递员

如何将图像文件从Angular发送到.net核心API

如何使用angular将文件发送到asp.net Web API?

如何使用 fetch 将 FormData 从 javascript 发送到 ASP.NET Core 2.1 Web API

如何使用ASP.NET MVC通过Ajax将选中的行发送到服务器?

如何从 .NET Core Web API 服务器 docker 容器将标签打印作业发送到网络 CUPS/Raspberry Pi 打印服务器?

在页面加载到asp.net之前将javascript值发送到服务器

将图像发送到.NET Web服务

无法使用log4javascript将客户端日志发送到服务器(Asp.net Web API)

将图像从 ASP.NET Web API 发送到 Angular 客户端

将值从 Windows 窗体应用程序发送到 Asp.Net Core Api

将 ViewModel 发送到 API ASP.net Core 2

将 ASP.NET MVC HttpContext 发送到 Web Api HttpContext

无法将整数集合发送到Web Core Api Post方法,该方法设置为null

如何将多个值从Asp.Net WebApi发送到Angularjs控制器?

如何将简单的值从axios.post发送到asp.net core?

如何将数据从JQuery Ajax发送到ASP.NET页?

如何将JavaScript对象发送到节点服务器

如何将多个json对象的数据发送到android中的服务器?

ASP.NET Web API服务

如何将数据从 HPA 发送到外部 API 服务

AngularJS:如何将数据发送到服务