Home Passing an array of Javascript classes to a MVC controller?
Reply: 0

Passing an array of Javascript classes to a MVC controller?

user6165
1#
user6165 Published in September 19, 2018, 6:49 pm

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

1 Answer 1

active oldest votes
up vote 3 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.336927 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO