如何通过ajax将对象数组从javascript传递到asp.net mvc函数?

时间:2021-07-23 22:57:35

I have the following javascript code that builds up an array of objects and i am trying to push this to an asp.net-mvc action through an ajax post. I am trying to figure out what is wrong with this below ?

我有下面的javascript代码,它构建了一系列对象,我正试图通过ajax post将其推到asp.net-mvc操作。我想弄清楚下面这个有什么问题?

javascript:

javascript:

var arr = [];
for (var i = 0; i < 5; i++) {
     var obj = {
        statusId: i,
        resizeable: true,
        rows: [1, 2, 3, 4, 5]
      };
      arr.push(obj);
}

$.ajax({
   type: 'POST',
   traditional: true,
   url: '/MyController/UpdateMe',
   data { info: arr },
   success: function () {  
       alert("complete");
   }
});

C# asp.net-mvc action:

c# asp.net-mvc行动:

public ActionResult UpdateMe(IEnumerable<UpdateInfo> info)
{
     foreach (var item in info)
     {
          Console.Write(item.statusIs + " has + " item.rows.length() + " items ");
     }
}

public class UpdateInfo
{
    public int statusId {get;set;}
    public bool resizable {get;set;}
    public List<int> rows {get;set;}
}

2 个解决方案

#1


3  

By default jQuery.ajax contentType is application/x-www-form-urlencoded. This means that parameters sent in url string. In your case, your array transformed to string: object&object&object.....

在默认情况下jQuery。ajax应用程序/ x-www-form-urlencoded contentType。这意味着在url字符串中发送的参数。在你的情况下,你的数组转换为字符串:object&object&object…

To change this behavoir, you should change contentType in jQuery.ajax to application/json and convert your js object to json with JSON.stringify:

要更改这个行为,应该在jQuery中更改contentType。ajax到application/json并将js对象与json .stringify:

$.ajax({
   type: 'POST',
   traditional: true,
   contentType: 'application/json',
   url: '/SchoolDetails/UpdateMe',
   data: JSON.stringify({ info: arr }),
   success: function () {  
      alert("complete");
   }
});

Action in debug:

行动在调试:

如何通过ajax将对象数组从javascript传递到asp.net mvc函数?

#2


0  

You cannot pass a JS array as a parameter in a POST request. You have to turn it into a string to pass it to the .NET action. Consider using jQuery.param() to do this. It will convert the object to a string like statusId=valueOfI&resizeable=true. Then you change it from a string back to an array after you receive it in the action (I can't suggest a function since I don't know .NET).

不能将JS数组作为POST请求的参数传递。你必须把它转换成一个字符串来传递给。net动作。考虑使用jQuery.param()进行此操作。它将把对象转换为一个字符串,如:雕塑sid =valueOfI&resizeable=true。然后,当您在操作中接收到它之后,您将它从一个字符串更改为一个数组(我不能建议一个函数,因为我不知道。net)。

#1


3  

By default jQuery.ajax contentType is application/x-www-form-urlencoded. This means that parameters sent in url string. In your case, your array transformed to string: object&object&object.....

在默认情况下jQuery。ajax应用程序/ x-www-form-urlencoded contentType。这意味着在url字符串中发送的参数。在你的情况下,你的数组转换为字符串:object&object&object…

To change this behavoir, you should change contentType in jQuery.ajax to application/json and convert your js object to json with JSON.stringify:

要更改这个行为,应该在jQuery中更改contentType。ajax到application/json并将js对象与json .stringify:

$.ajax({
   type: 'POST',
   traditional: true,
   contentType: 'application/json',
   url: '/SchoolDetails/UpdateMe',
   data: JSON.stringify({ info: arr }),
   success: function () {  
      alert("complete");
   }
});

Action in debug:

行动在调试:

如何通过ajax将对象数组从javascript传递到asp.net mvc函数?

#2


0  

You cannot pass a JS array as a parameter in a POST request. You have to turn it into a string to pass it to the .NET action. Consider using jQuery.param() to do this. It will convert the object to a string like statusId=valueOfI&resizeable=true. Then you change it from a string back to an array after you receive it in the action (I can't suggest a function since I don't know .NET).

不能将JS数组作为POST请求的参数传递。你必须把它转换成一个字符串来传递给。net动作。考虑使用jQuery.param()进行此操作。它将把对象转换为一个字符串,如:雕塑sid =valueOfI&resizeable=true。然后,当您在操作中接收到它之后,您将它从一个字符串更改为一个数组(我不能建议一个函数,因为我不知道。net)。