admin管理员组文章数量:1287597
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-mvc controller action
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:
- null
- array of 3 items but the values of Id are always 0 and the value of Name is always an empty string
any suggestions on what I am doing wrong here?
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-mvc controller action
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:
- null
- array of 3 items but the values of Id are always 0 and the value of Name is always an empty string
any suggestions on what I am doing wrong here?
Share Improve this question edited Dec 20, 2015 at 15:36 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 23, 2015 at 11:34 leoraleora 197k368 gold badges906 silver badges1.4k bronze badges 1- 1 possible duplicate of jQuery Ajax POSTing array to ASP.NET MVC Controller – Ilya Sulimanov Commented Mar 23, 2015 at 11:42
3 Answers
Reset to default 4You javascipt object properties do not have indexers, so you need to use the ajax contentType: "application/json"
option and stringify the object
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() {`
My personal favorite is using JSON.NET
(available through NuGet
).
You may define on your Action that it would expect a JObject
or JArray
.
Then in the Action you just cast your input to your expected type.
From js perspective you just do your post normally.
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;}
}
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({
url: "/MyController/Update",
type: "POST",
datatype: 'json',
contentType: "application/json",
data: JSON.stringify(myArray),
success: function (data) {
alert("Complete");
}
});
本文标签:
版权声明:本文标题:javascript - What is the correct way to post array of objects using query to an asp.net-mvc controller action? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741300211a2371049.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论