3

I am trying to send collection of JavaScript objects to my API service, But server receive empty object list!

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

Code in 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);
    }

Any help please ?

5
  • Just send the items in array. Try to do that. and see how it hits it. Commented Sep 30, 2015 at 13:23
  • Have you taken a look at the XHR object from the AJAX call, change your sucess object to : success:function(data, status, xhr){/*break here or use alert */ alert(xhr.responseText);}..... Commented Sep 30, 2015 at 13:27
  • @Casey it worked with array but I need it in Collection, so I can reach it by key Commented Sep 30, 2015 at 13:33
  • @nixxbb Thanks, But I made a breakpoint on asp.net api post function to check the data first, The AJAX call reach the asp.net function but not send the data! Commented Sep 30, 2015 at 13:36
  • 1
    Not sure about Collections and JSON formatting, I always create objects var obj = {key:val} and add them to an array. Then I recall having some troubles parsing the JSON into a List, but it's been a while and I guess I'm not as familiar with what you are doing as I thought, sorry... Commented Sep 30, 2015 at 13:44

2 Answers 2

2

I found away to solve the issue by convert the collection to array, I know it is not the best solution, And I hope someone could find better solution that avoid use array, but until someone give a better answer, I put my way to fix that as below: I have added the function toArr // means covert collection to array as below:

//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 function :

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

Hope that help someone in future...

Sign up to request clarification or add additional context in comments.

Comments

1

You can add method inside your Collection for converting you local collection to JSON object.

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]);
                }
            }
        }
    }

Hope, it will help. Also suggestion: do not use objects for storing values, because they are slower then arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.