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

Javascript 如何将JS对象集合发送到ASP.NET API服务?,javascript,c#,list,asp.net-web-api,collections,Javascript,C#,List,Asp.net Web Api,Collections,我试图将JavaScript对象的集合发送到我的API服务,但服务器接收到空的对象列表 <script> //Collection var Collection = function () { this.count = 0; this.collection = {}; this.add = function (key, item) { if (this.collection[key] != u

我试图将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>

//收藏
变量集合=函数(){
此值为0.count;
this.collection={};
this.add=函数(键,项){
if(this.collection[key]!=未定义)
返回未定义;
此.集合[键]=项;
返回++this.count
}
this.remove=功能(键){
if(this.collection[key]==未定义)
返回未定义;
删除此。集合[键]
return--this.count
}
this.item=功能(键){
返回此.集合[键];
}
this.forEach=函数(块){
for(在此.collection中输入){
if(this.collection.hasOwnProperty(键)){
块(此集合[键]);
}
}
}
}
//食物的JavaScript类
功能食品目录(编号、数量、名称、CAL、carb、fat、prt、unt){
这个。否=否;
this.qunt=qunt;
this.name=名称;
this.cals=cals;
这个.carb=carb;
这个。脂肪=脂肪;
this.prt=prt;
this.unt=unt;
}
//实例化新的obj
var fod=新的食物含量(3,5,'SomeName',300,180,100,20,'Gram');
var-fno=333;
var timCol=新集合();
添加(fno,fod);
var urlpaths='/api/FoodServ';
$.ajax({
url:urlpaths,
方法:“张贴”,
contentType:'application/json;charset=utf-8',
数据:JSON.stringify(timCol),
成功:功能(数据){
//任何事
}
});
ASP.NET API中的代码:

      [HttpPost]
    public HttpResponseMessage Post(List<foodcls> fods) // Here fods is empty
    {
        int rslt=0;
        string UserId = "sam@am.com";//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);
    }
[HttpPost]
public HttpResponseMessage Post(List fods)//此处fods为空
{
int rslt=0;
字符串UserId=”sam@am.com“;//User.Identity.Name;
List fooditems=新列表();
如果(fods.Count()>0)
{
foreach(fods中的foodcls项目)
{
Foods_-fine_-TBL fooditem=新食品_-fine_-TBL();
fooditem.FoodNo=项目编号;
fooditem.Quantity=item.Quant;
fooditem.PersonID=用户ID;
添加(fooditem);
}
}
rslt=SaveAllItems(fooditems);//rslt餐号
返回请求.CreateResponse(HttpStatusCode.OK,rslt);
}

有什么帮助吗?

您可以在集合中添加方法,将本地集合转换为JSON对象

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.toJSON = function(){
            return JSON.stringify(this.collection);
        }
        this.forEach = function (block) {
            for (key in this.collection) {
                if (this.collection.hasOwnProperty(key)) {
                    block(this.collection[key]);
                }
            }
        }
    }

希望这会有帮助。另外建议:不要使用对象来存储值,因为它们是。

我发现通过将集合转换为数组来解决这个问题,我知道这不是最好的解决方案,我希望有人能找到更好的解决方案来避免使用数组,但在有人给出更好的答案之前,我会按照以下方式解决这个问题: 我已将函数添加到arr//means covert collection到数组中,如下所示:

//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)

希望将来能帮助别人…

只需发送数组中的项目即可。试着这样做。看看它是如何运行的。你有没有从AJAX调用中查看过XHR对象,将你的success对象更改为:success:function(data,status,XHR){/*在这里中断或使用alert*/alert(XHR.responseText);}….@Casey它与数组一起工作,但我在集合中需要它,所以我可以通过key@nixxbb谢谢但是我在asp.net api post函数上做了一个断点,首先检查数据,AJAX调用到达asp.net函数,但不发送数据!不确定集合和JSON格式,我总是创建对象var obj={key:val},并将它们添加到数组中。然后我回忆起在将JSON解析成一个列表时遇到了一些问题,但是已经有一段时间了,我想我对你正在做的事情不像我想的那样熟悉,对不起。。。