Ajax Jquery Post数组未被转换为C#List

时间:2015-02-18 07:54:30

标签: jquery ajax asp.net-mvc

我们使用KO.JS进行数据绑定,我们有可观察的数组。我们使用Ajax Jquery Post将数据发布到服务器上。

由于某种原因,JSON中的数组未转换为c#List。我们得到NULL。

以下是剪辑,有人可以帮忙吗?

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

最后C#:

[HttpPost]
        public virtual ActionResult Update(TeacherDetails teacherDetails)


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

同样在什么类型的场景中我们使用json.stringnify?

2 个答案:

答案 0 :(得分:1)

将数据发送到服务器时,您应使用ContentType指定要发送的内容的默认类型(默认值:&#39; application / x-www-form-urlencoded; charset = UTF-8&#39;)< / p>

dataType是您期望从服务中获得的,请参阅jQuery ajax()api以获取更多信息

对于JSON:

Content-Type: application/json

对于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) {

}

); 我通常在设置帖子和回调时使用Postman进行测试。还要检查你的哑剧类型是否正确!

答案 1 :(得分:0)

你说在评论中你的添加方法没有任何问题,也许它很容易被遗漏,因为你有很多名字相似的变量。

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()?
};

试试这个

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