0

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?

2
  • When do you actually add ids to the removedIds array? you have another variable called removedScIds . Does the data look ok when you do console.log(teacherDetails)? Commented Feb 18, 2015 at 8:02
  • No issues with add method. Tried adding via console but still null at MVC side. Commented Feb 18, 2015 at 8:28

2 Answers 2

1

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!

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

Comments

0

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

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.