I am trying to pass an array of services to my controller. I've tried a bunch of different ways to get it work, serializing the data before going to controller, serializing each service, only thing that seems to work is changing the controller parameter to string and serializing array, then using JsonConvert, but I'd rather not do that.

With the specified code, I am getting the correct number of items in the List, but they all contain a service id with an empty guild, and service provider id is null.

Any ideas?

Javascript


function ServiceItem() {
    this.ServiceProviderID = 'all';
    this.ServiceID = '';
}

var selecteditems= (function () {

    var services = new Array();

    return {
       all: function() { 
           return services; 
       },
       add: function(service) {
           services.push(service);
       }
    };

})();

var reserved = [];
$.each(selecteditems.all(), function(index, item){
   reserved.push({ ServiceID: item.ServiceID, ServiceProviderID: item.ServiceProviderID});
});

getData('Controller/GetMethod', { items: reserved }, function(result) {
});

var getData = function (actionurl, da, done) {
       $.ajax({
           type: "GET",
           url: actionurl,
           data: da,
           dataType: "json",
           async: true,
           success: function (d) {
               if (typeof (done) == 'function') {
                   var str = JSON.stringify(d);
                   done(JSON.parse(str));
               }
           }
       });
};


Controller

public JsonResult GetMethod(List<CustomObject> items)
{
}

Custom Object

public class CustomObject
{
   public Guid ServiceID {get;set;}
   public Guid? ServiceProviderID {get;set;}
}
share|improve this question
up vote 3 down vote accepted

Set the content-type and use POST instead of GET (as it is a list of complex type objects). mark your action with HttpPost attribute too.

See if this works:-

 $.ajax({
           type: "POST",
           url: actionurl,
           data: JSON.stringify(da),
           dataType: "json",
           contentType: 'application/json',
           async: true,
           success: function (d) {
               if (typeof (done) == 'function') {
                   var str = JSON.stringify(d);
                   done(JSON.parse(str));
               }
           }
       });
share|improve this answer
    
yeah sorry, i didnt give my exact code, let me fix it – jaekie May 29 '13 at 22:16
    
getData is actually a method I use for all of my database calls.. but I was passing { items: reserved } to it.. – jaekie May 29 '13 at 22:20
    
traditional:true, makes list contain 0 rows, was 2 rows before but empty values – jaekie May 29 '13 at 22:22
    
@csharpdev probably because of the complex object type use POST instead of GET. – PSL May 29 '13 at 22:26
    
@csharpdev See my update. this should work for you. – PSL May 29 '13 at 22:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.