Nil Pun
Nil Pun

Reputation: 17373

Ajax Jquery Post array is not being translated to C# List

We are using KO.JS for data binding where we have observable array. We use Ajax Jquery Post to post the data on to server.

For some reason the array from JSON is not being converted on to c# List. We get NULL.

Below is the snipped, could someone please help?

var removedIds = [];            
            for (var rsId in RemovedSchoolIds()) {                
                removedScIds.push(inputForm.schoollViewModel.RemovedSchoolIds());
            }
            removedScIds = removedScIds.join();
            var teacherDetails = {
                    FirstName: inputForm.formDetails.firstName(),
                    LastName: inputForm.formDetails.lastName(),
                    RemoveFromSchools: removedIds.toString()
            };



dataService.UpdateTeacher(teacherDetails);

Data Service has UpdateTeacher which has:

$.ajax({
                    type: "POST",
                    url: service/update,
                    data: $.param(data),
                    dataType: 'json',
                    timeout: timeout
                })
                .done(successfulCallback)
                .fail(unsuccessfulCallback)
                .always(alwaysCallback);

And Finally C#:

[HttpPost]
        public virtual ActionResult Update(TeacherDetails teacherDetails)


 public class TeacherDetails 
    {
        /// <summary>
        /// Guid array for remove schools.
        /// </summary>        
        public IEnumerable<Guid> RemoveFromSchools { get; set; }
    }

Also on what type of scenario do we use json.stringnify?

Upvotes: 0

Views: 113

Answers (2)

Doff3n
Doff3n

Reputation: 664

When sending data to the server you should use ContentType to specify the mime type of what you are sending (default: 'application/x-www-form-urlencoded; charset=UTF-8')

dataType is what you expect back from the service, see jQuery ajax() api for more info

For JSON:

Content-Type: application/json

For JSON-P:

Content-Type: application/javascript

e.g.

$.ajax({
    type: "POST",
    url: "service/update",
    data: JSON.stringify($.param(data)),
    datatype : "json",
    contentType: "application/json",
success : function() {

},
error : function(error) {

}

); I usually test with Postman when setting up posts and callbacks. Also check that your mime types are correct!

Upvotes: 1

Martin Larsson
Martin Larsson

Reputation: 424

You said there was nothing wrong with your add method in a comment, maybe it's easy to miss because you have so many variables with similar names.

var removedIds = []; //the array you use with teacherDetail parameters.    

for (var rsId in RemovedSchoolIds()) {            
   //now you add values to another array declared somewhere else - and this looks like you add the same values all over in the loop    
   removedScIds.push(inputForm.schoollViewModel.RemovedSchoolIds());

}

//not sure why you do this, still not the array you are using in your parameters
removedScIds = removedScIds.join(); 

var teacherDetails = {
    FirstName: inputForm.formDetails.firstName(),
    LastName: inputForm.formDetails.lastName(),
    RemoveFromSchools: removedIds.toString() //this is an empty array and why toString()?
};

try this maybe

var removedIds = [];
//not sure what RemovedSchoolIds() does
for (var rsId in RemovedSchoolIds()) {                
     removedIds.push(rsId); 
}
var teacherDetails = {
        FirstName: inputForm.formDetails.firstName(),
        LastName: inputForm.formDetails.lastName(),
        RemoveFromSchools: removedIds
    };

Upvotes: 0

Related Questions