javascript 使用查询将对象数组发布到 asp.net-mvc 控制器操作的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29209204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:13:50  来源:igfitidea点击:

What is the correct way to post array of objects using query to an asp.net-mvc controller action?

javascriptjqueryajaxasp.net-mvchttp-post

提问by leora

I have an array of data:

我有一组数据:

var myArray = [{"id": 1, "name", "John"},{"id": 2, "name", "Joe"},{"id": 3, "name", "Bill"}] 

 $.post("/MyController/Update", myArray, function (data) {
       alert("Complete");
 });

and here is my asp.net-mvc controller action

这是我的 asp.net-mvc 控制器操作

public ActionResult Update(List<Person> people)
{
     return Json(new {Success = true});
}

public class Person
{
    public int id {get;set;}
    public string name {get;set;}
}

I also tried:

我也试过:

 var paramString = JSON.stringify({ people: myArray});

 $.post("/MyController/Update", paramString , function (data) {
        alert("Complete");
  });

but when i look at the people parameter on the server, I either see:

但是当我查看服务器上的 people 参数时,我要么看到:

  1. null
  2. array of 3 items but the values of Id are always 0 and the value of Name is always an empty string
  1. 空值
  2. 包含 3 个项目的数组,但 Id 的值始终为 0,Name 的值始终为空字符串

any suggestions on what I am doing wrong here?

关于我在这里做错的任何建议?

回答by

You javascipt object properties do not have indexers, so you need to use the ajax contentType: "application/json"option and stringify the object

您的 javascipt 对象属性没有索引器,因此您需要使用 ajaxcontentType: "application/json"选项并将对象字符串化

var myArray = [{"id": 1, "name", "John"},{"id": 2, "name", "Joe"},{"id": 3, "name", "Bill"}] 

$.ajax({
  type: 'post',
  url: '@Url.Action("Update", "MyController")', // don't hardcode your url's!
  contentType: "application/json; charset=utf-8",
  data: JSON.stringify({ people: myArray }),
  success: function (data) {
    ....
  }
})

Side note. The following object would post back

边注。以下对象将回发

var myArray = { people[0].id: 1, people[0].name: "John", people[1].id: 2, people[1].name: "Joe" }

using

使用

$.post('@Url.Action("Update", "MyController")', myArray, function() {`

回答by Giannis Paraskevopoulos

My personal favorite is using JSON.NET(available through NuGet).

我个人最喜欢使用JSON.NET(可通过 获得NuGet)。

You may define on your Action that it would expect a JObjector JArray.

你可以在你的 Action 上定义它期望 aJObjectJArray

Then in the Action you just cast your input to your expected type.

然后在 Action 中,您只需将输入转换为您期望的类型。

From js perspective you just do your post normally.

从 js 的角度来看,您只需正常发布您的帖子。

Example:

例子:

public ActionResult Update(JArray input)
{
     var persons = input.Select(x=>x.ToObject<Person>()) // This will return an `Enumerable<Person>`
     //Do your other stuff here
     return Json(new {Success = true});
}

public class Person
{
    public int id {get;set;}
    public string name {get;set;}
}

回答by Vinod Bangar

try this

试试这个

First change your array like this

首先像这样改变你的数组

var myArray = [{"id": 1, "name": "John"},{"id": 2, "name": "Joe"},{"id": 3, "name": "Bill"}] ;

And then try posting using $.ajax()

然后尝试使用 $.ajax() 发布

$.ajax({
    url: "/MyController/Update",
    type: "POST",
    datatype: 'json',
    contentType: "application/json",
    data: JSON.stringify(myArray),
    success: function (data) {
          alert("Complete");
    }
});