传递到MVC控制器的方法使用jQuery的Ajax对象的列表 [英] Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax
问题描述
我试图对象的数组传递到使用MVC控制器方法
jQuery的阿贾克斯()函数。当我进入PassThing()C#控制器的方法,
参数东西为空。我已经使用列表类型尝试这个
的说法,但这并不能工作。我在做什么错了?
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
VAR的事情= [
{ID:1,颜色:黄},
{ID:2,颜色:蓝},
{ID:3,颜色:红色}
]; $阿贾克斯({
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON,
输入:POST,
网址:'/ XHR / ThingController / PassThing',
数据:JSON.stringify(的东西)
});
});
< / SCRIPT>公共类ThingController:控制器
{
公共无效PassThing(事[]的东西)
{
//做的事情在这里的东西...
} 公共类的事
{
公众诠释ID {搞定;组; }
公共串色{搞定;组; }
}
}
使用NickW的建议下,我能得到这个使用 =东西JSON.stringify({东西:事情})工作;
下面是完整的code。
$(文件)。就绪(函数(){
VAR的事情= [
{ID:1,颜色:黄},
{ID:2,颜色:蓝},
{ID:3,颜色:红色}
]; 事情= JSON.stringify({东西:事情}); $阿贾克斯({
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON,
输入:POST,
网址:'/主页/ PassThings',
数据:事情,
成功:函数(){
$('#结果)HTML('PassThings()调用成功。')。
},
故障:功能(响应){
$('#结果)HTML(响应);
}
});
});
公共无效PassThings(列表<&东西GT;的东西)
{
VAR T =东西;
}公共类的事
{
公众诠释标识{搞定;组; }
公共串色{搞定;组; }
}
有两件事情我从中学到:
1)的contentType和数据类型的设置是在阿贾克斯()函数绝对必要的。如果缺少它不会工作。我多试错后,发现了这一点。 2)要传递对象数组到一个MVC控制器的方法,只需使用JSON.stringify({东西:事情})格式
我希望这可以帮助别人!
I'm trying to pass an array of objects into an MVC controller method using jQuery's ajax() function. When I get into the PassThing() C# controller method, the argument "things" is null. I've tried this using a type of List for the argument, but that doesn't work either. What am I doing wrong?
<script type="text/javascript">
$(document).ready(function () {
var things = [
{ id: 1, color: 'yellow' },
{ id: 2, color: 'blue' },
{ id: 3, color: 'red' }
];
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '/Xhr/ThingController/PassThing',
data: JSON.stringify(things)
});
});
</script>
public class ThingController : Controller
{
public void PassThing(Thing[] things)
{
// do stuff with things here...
}
public class Thing
{
public int id { get; set; }
public string color { get; set; }
}
}
Using NickW's suggestion, I was able to get this working using things = JSON.stringify({ 'things': things });
Here is the complete code.
$(document).ready(function () {
var things = [
{ id: 1, color: 'yellow' },
{ id: 2, color: 'blue' },
{ id: 3, color: 'red' }
];
things = JSON.stringify({ 'things': things });
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '/Home/PassThings',
data: things,
success: function () {
$('#result').html('"PassThings()" successfully called.');
},
failure: function (response) {
$('#result').html(response);
}
});
});
public void PassThings(List<Thing> things)
{
var t = things;
}
public class Thing
{
public int Id { get; set; }
public string Color { get; set; }
}
There are two things I learned from this: 1) The contentType and dataType settings are absolutely necessary in the ajax() function. It won't work if they are missing. I found this out after much trial and error. 2) To pass in an array of objects to an MVC controller method, simply use the JSON.stringify({ 'things': things }) format.
I hope this helps someone else!
这篇关于传递到MVC控制器的方法使用jQuery的Ajax对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!