【发布时间】:2014-06-10 12:14:29
【问题描述】:
我想要做的可能很简单,但是我对 jQuery 不是很熟悉,我不知道该怎么做。
我想将一些数据作为 JSON 发送到 ASP.NET 控制器。数据包含一些字符串和对象列表。
代码看起来有点像这样:
查看:
$(document).ready(function () {
var stuff = [
{ id: 1, option: 'someOption' },
{ id: 2, option: 'someOther' },
{ id: 3, option: 'anotherOne' }
];
things = JSON.stringify({ 'things': things });
var dataRow = {
'String1': 'A String',
'String2': 'AnotherOne'
}
dataRow = JSON.stringify(dataRow);
var sendData = dataRow + things;
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '/Backend/DoStuffWithStuff',
data: sendData,
success: function () {
alert('Success!');
},
failure: function (response) {
alert('Fail! :(');
}
});
});
控制器:
public class Stuff
{
public int id { get; set; }
public string option{ get; set; }
}
public void DoStuffWithStuff(string String1, String2, List<Thing> things)
{
//Do my Stuff
}
任何想法都会很棒! :)
【问题讨论】:
标签: javascript jquery asp.net json controller