通过jQuery ajax将JSON对象的数组发送到MVC3操作方法(Posting array of JSON objects to MVC3 action method via jQuery ajax)
模型联编程序是否支持JSON对象的数组? 下面的代码在将单个JSON域对象作为ajax帖子的一部分发送时工作。 但是,在发送JSON域对象数组时,action参数为null。
var domains = [{
DomainName: 'testt1',
Price: '19.99',
Available: true
}, {
DomainName: 'testt2',
Price: '15.99',
Available: false
}];
$.ajax({
type: 'POST',
url: Url.BasketAddDomain,
dataType: "json",
data: domains,
success: function (basketHtml) {
},
error: function (a, b, c) {
alert('A problem ocurred');
}
});
这是行动方法:
public ActionResult AddDomain(IEnumerable domain)
{
...
任何想法,如果有可能做到这一点?
编辑
@Milimetric
您的解决方案有效 但是,这是我的错,但是我演示的代码并不是我的问题的实际代码,我试图展示更易于理解的等效代码。
我实际上正在创建一个数组,然后交互一些DOM元素并将JSON对象推入数组,然后将此数组作为数据发布......
var domains = [];
$(this).parents('table').find('input:checked').each(function () {
var domain = {
DomainName: $(this).parent().parent().find('.name').html(),
Price: $(this).parent().parent().find('.price span').html(),
Available: $(this).parent().parent().find('.available').html() == "Available"
}
domains.push(domain);
});
$.ajax({
type: 'POST',
url: Url.BasketAddDomain,
dataType: "json",
data: { domain: domains },
success: function (basketHtml) {
},
error: function (a, b, c) {
alert('A problem ocurred');
}
});
Does the model binder not suport arrays of JSON objects? The code below works when sending a single JSON domain object as part of the ajax post. However, when sending an array of JSON domain objects, the action parameter is null.
var domains = [{
DomainName: 'testt1',
Price: '19.99',
Available: true
}, {
DomainName: 'testt2',
Price: '15.99',
Available: false
}];
$.ajax({
type: 'POST',
url: Url.BasketAddDomain,
dataType: "json",
data: domains,
success: function (basketHtml) {
},
error: function (a, b, c) {
alert('A problem ocurred');
}
});
This is the action method:
public ActionResult AddDomain(IEnumerable domain)
{
...
Any ideas if it is possible to do this?
EDIT
@Milimetric
Your solution works! However, this is my fault, but the code I demonstrated isn't the real code of my problem, I was trying to show equivalent code that is easier to understand.
I am actually creating an array, then interating some DOM elements and pushing a JSON object onto the array, then posting this array as the data...
var domains = [];
$(this).parents('table').find('input:checked').each(function () {
var domain = {
DomainName: $(this).parent().parent().find('.name').html(),
Price: $(this).parent().parent().find('.price span').html(),
Available: $(this).parent().parent().find('.available').html() == "Available"
}
domains.push(domain);
});
$.ajax({
type: 'POST',
url: Url.BasketAddDomain,
dataType: "json",
data: { domain: domains },
success: function (basketHtml) {
},
error: function (a, b, c) {
alert('A problem ocurred');
}
});
原文:https://stackoverflow.com/questions/6031206
更新时间:2019-05-29 00:40
最满意答案
你需要:
var domains = { domains: [... your elements ...]};
$.ajax({
type: 'post',
url: 'Your-URI',
data: JSON.stringify(domains),
contentType: "application/json; charset=utf-8",
traditional: true,
success: function (data) {
...
}
});
通常,在调试器中检查Request对象,您将看到正在传递的内容,并了解如何“说出”HTTP。
注意 :刚刚发现了这一点。 模型活页夹扼制可空的十进制属性,如:
public decimal? latitude { get; set; }
所以如果你用一个看起来像这样的json字符串发布它,它不会绑定它:
{"latitude":12.0}
但是,如果你发布这样的东西,它会工作:
{"latitude":"12.0"}
You need:
var domains = { domains: [... your elements ...]};
$.ajax({
type: 'post',
url: 'Your-URI',
data: JSON.stringify(domains),
contentType: "application/json; charset=utf-8",
traditional: true,
success: function (data) {
...
}
});
In general, check out the Request object in the debugger, you'll see what's being passed and get an idea of how to "speak" HTTP.
NOTE: Just found this out. The model binder chokes on nullable decimal properties like:
public decimal? latitude { get; set; }
So it won't bind that if you're posting to it with a json string that looks like this:
{"latitude":12.0}
But it WILL work if you post something like this:
{"latitude":"12.0"}
相关问答
你需要: var domains = { domains: [... your elements ...]};
$.ajax({
type: 'post',
url: 'Your-URI',
data: JSON.stringify(domains),
contentType: "application/json; charset=utf-8",
...
据推测,你点击的是一个URL。 您没有阻止在您的点击事件中发生默认事件(加载网址) $('#user_dialog .create').click(function (event) {
event.preventDefault();
//rest of function
每当点击事件结束时,默认事件(如果是链接则加载URL,如果是提交按钮则提交表单,切换复选框等)将会发生。 为了防止这种情况,您必须使用preventDefault()或返回false显式地执行此操作。 有关文档,
...
所以,解决这个特定问题的方法是在SO的其他地方找到两个或更多解决方案的组合(其中一个可能是Jakub的回答围绕使用通用集合类型 - IEnumerable - 而不是更具体的 - List ) 。 首先,服务器端代码不需要改变 - 所有这一切都很好。 所有需要进行的更改都是客户端,特别是jQuery和Javascript如何将数据发送到服务器。 这里是旧的Javascript: $.ajax({
type: 'POST',
url: '/Action/Method',
da
...
你可以跳过var声明和stringify。 否则,这将工作正常。 $.ajax({
url: '/home/check',
type: 'POST',
data: {
Address1: "423 Judy Road",
Address2: "1001",
City: "New York",
State: "NY",
ZipCode: "10301",
Country: "USA
...
试试这个..位置应该是string.Be肯定会字符串。 function getLocationPictures(location) {
var pics;
$.ajax({
type : "POST",
url : "/Home/GetLocationPictures",
data : JSON.stringify({'location' : location}),
contentType : "application/json; charset=utf-8",
dataType : 'json
...
你永远得到“成功”是正确的,因为你的要求总是有效的。 $.ajax不知道数据是什么回来。 所以只有当你的请求失败时才会出错(例如404) 你必须这样做 $.ajax({
type: "POST",
url: "@(Url.Action("AX_Login","Users"))",
data:$("#MiniLoginForm").serialize(),
success: function (result)
{
if (result.suc
...
你必须重写你的数据初始化: var employees = {
accounting: [ // accounting is an array in employees.
{
firstName: "", // First element
lastName: "Doe",
...
您最好将它们作为单独的参数发送到操作方法,而不是将其组合到另一个数组中,如下所示: 假设我们仍然有你的两个数组: var features = new Array();
var menuItems = new Array();
menuItems.push({ "Name": $(this).attr('name'), "isChecked": isChecked });
features.push({ "Name": $(this).attr('name'), "isChecked": isCh
...
啊哈,我认为它是因为你在现有的变更事件之上定义了另一个jQuery(this).change事件。 这是最初的改变事件 $('#category_1').change(function () {
然后在你的辅助函数里面 jQuery(this).change(function () {
我认为这是在断言; 只有运行以下代码才会更改并且选择没有再次更改以触发它。 尝试以下修订版。 $.fn.cascade = function (options) {
var defaults = {};
...
您无法使用GET动词发送JSON编码的请求。 所以用type: 'POST'替换type: 'GET' type: 'POST' ,它会起作用。 此外,由于您已经指定了JSON请求,因此您必须发送使用JSON.stringify函数实现的JSON请求: data: JSON.stringify({ gameId: gameId, platformId: platformId }), 但由于你只有2个值,我认为使用GET会更容易。 所以我的建议是删除contentType: 'application
...