0

I have an AngularJS directive which returns me an array with some values, like this below:

enter image description here

The AngularJS code I'm using to generate this object like above is:

//--------------------------
// service...

service.getSelectedOptions = function() {
    var options = [];
    for (var I = 0; I < service.optionList.length; ++I) {
        var availableOption = service.optionList[I];

        if (availableOption.selecionado !== '') {
            options.push(availableOption);
        }
    }
    return opcoes;
};

//--------------------------
// controller...
var options = [];
var list = OptionsService.getSelectedOptions();
for (var I = 0; I < list.length; ++I) {
    var option = list[I];
    options.push({ Handle: option.Handle, Selecionado: option.Selecionado });
}
console.log(options);

// Sending the values to backend...
doPost('..../SaveOptions', { options: options }, function (result) {  });

Ok, I created a ViewModel class to receive those objects into my controller.

public class OptionsViewModel {
    public int Handle { get; set; }
    public string Selecionado { get; set; }
}

My controller is declared like this below:

public JsonResult SaveOptions(OptionsViewModel[] options) {
    //...
}

The problem is: if I chose 4 options, the array in the backend has 4 options, but the values are not binded to the objects.

enter image description here

Why that? Anyone knows why? Thanks!!

Kiwanax
  • 1,265
  • 1
  • 22
  • 41
  • Can you post the method for `OptionsService.getSelectedOptions();` – tymeJV Apr 03 '14 at 12:39
  • @tymeJV The code is added. – Kiwanax Apr 03 '14 at 12:44
  • Is anything in that service a data call to the backend? – tymeJV Apr 03 '14 at 12:45
  • If you look at the network tab of your browser, what is the actual POST being sent to the controller? – Jaime Torres Apr 03 '14 at 12:46
  • @JaimeTorres the SaveOptions POST method is sent to the controller. The POST is executing properly, the problem is to bind the JS object values to the ViewModel array as parameter from my controller method. – Kiwanax Apr 03 '14 at 12:49
  • @tymeJV No, it just gets some data I added into the `$scope` from my controller's screen. – Kiwanax Apr 03 '14 at 12:50
  • @Kiwanax right, but sometimes it's easy to see what the issue is if you interrogate the post. This is normally easier than customizing the model-binder to see what's coming in. – Jaime Torres Apr 03 '14 at 12:58
  • @JaimeTorres got it. The first screenshot is about the object was generated by the Javascript. I'm not understanding WHY it's not being binded properly to the viewmodel. – Kiwanax Apr 03 '14 at 13:08
  • But, can you post what the POST is? Are you using the $http service? If you are using MVC (not WebAPI), it needs contentType to be explicitly 'application/json' with a 'json' dataType – Jaime Torres Apr 03 '14 at 13:09
  • @JaimeTorres I'm using jQuery AJAX. "doPost" is a function similar to $.post from jQuery lib. The problem isn't in the lib. Other kind of data is send successfully, except these options array. – Kiwanax Apr 03 '14 at 13:26
  • I understand. I've just seen this problem several times before: http://stackoverflow.com/questions/19768484/how-to-get-ajax-posted-array-in-my-c-sharp-controller/19768563#19768563 Interrogating the actual POST will tell you if you are sending the appropriate headers or not for MVC. – Jaime Torres Apr 03 '14 at 13:36
  • @JaimeTorres Yeah! Setting the `contentType` the list is properly bound. Thanks!!! – Kiwanax Apr 03 '14 at 14:06

1 Answers1

1

The solution was modify two parameters in the AJAX call:

  1. Set contentType: "application/json";
  2. Use JSON.stringify(parameters) to the parameters.

Thanks!

Kiwanax
  • 1,265
  • 1
  • 22
  • 41