ASP.NETMVC+;提交+;JSON

ASP.NETMVC+;提交+;JSON,.net,asp.net-mvc,ajax,json,.net,Asp.net Mvc,Ajax,Json,我需要在一个请求中发送模型数据和JSON数据。这意味着什么: var CurrentOrder = [{ 'ProviderAn': $("#DdlProviders").val(), 'ItemAn': $("#DdlItems").val() }]; 如果使用此函数发送JSON数据: $("#SaveOrder").click(function () {

我需要在一个请求中发送模型数据和JSON数据。这意味着什么:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
如果使用此函数发送JSON数据:

    $("#SaveOrder").click(function () {
        $.ajax({
            url: '/Manager/AddOrder',
            type: 'POST',
            dataType: 'json',
            data: $.toJSON(ResultArray),
            contentType: 'application/json; charset=utf-8'
        });
    });
var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
我有

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
public ActionResult AddOrder(SUPNew.Models.Order newOrder,List ResultArray)

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];

这是MVC 2,别忘了设置请求内容类型:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
$('#SaveOrder').click(function () {
    $.ajax({
        url: '/Manager/AddOrder',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify({
            newOrder: { orderProp1: 'value1', orderProp2: 'value2' },
            resultArray: [
                { orderListProp1: 'value1', orderListProp2: 'value2' },
                { orderListProp1: 'value3', orderListProp2: 'value4' },
                { orderListProp1: 'value5', orderListProp2: 'value6' }
            ]
        }),
        contentType: 'application/json; charset=utf-8',
        success: function(result) {
            // the request succeeded
        }
    });

    // Don't forget to cancel the default action or if SaveOrder
    // is some link you might get redirected before the 
    // AJAX call ever had time to execute
    return false;
});
现在在服务器端,您可能需要一个新的视图模型来聚合这两个对象:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
public class MyViewModel
{
    public Order NewOrder { get; set; }
    public List<OrderList> ResultArray { get; set; }
}
另外,在服务器端,除非您使用ASP.NET 3.0(默认情况下支持JSON),否则您需要编写一个脚本

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];

更新:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
您可以使用将表单序列化为JSON:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
然后:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
$.ajax({
    url: '/Manager/AddOrder',
    contentType: 'application/json',
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify({
        newOrder: $('#SaveOrder').serializeObject(),
        resultArray: [
            { orderListProp1: 'value1', orderListProp2: 'value2' },
            { orderListProp1: 'value3', orderListProp2: 'value4' },
            { orderListProp1: 'value5', orderListProp2: 'value6' }
        ]
    }),
    contentType: 'application/json; charset=utf-8'
});

不要忘记设置请求内容类型:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
$('#SaveOrder').click(function () {
    $.ajax({
        url: '/Manager/AddOrder',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify({
            newOrder: { orderProp1: 'value1', orderProp2: 'value2' },
            resultArray: [
                { orderListProp1: 'value1', orderListProp2: 'value2' },
                { orderListProp1: 'value3', orderListProp2: 'value4' },
                { orderListProp1: 'value5', orderListProp2: 'value6' }
            ]
        }),
        contentType: 'application/json; charset=utf-8',
        success: function(result) {
            // the request succeeded
        }
    });

    // Don't forget to cancel the default action or if SaveOrder
    // is some link you might get redirected before the 
    // AJAX call ever had time to execute
    return false;
});
现在在服务器端,您可能需要一个新的视图模型来聚合这两个对象:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
public class MyViewModel
{
    public Order NewOrder { get; set; }
    public List<OrderList> ResultArray { get; set; }
}
另外,在服务器端,除非您使用ASP.NET 3.0(默认情况下支持JSON),否则您需要编写一个脚本

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];

更新:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
您可以使用将表单序列化为JSON:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
然后:

var CurrentOrder =
            [{
                'ProviderAn': $("#DdlProviders").val(),
                'ItemAn': $("#DdlItems").val()
            }];
$.ajax({
    url: '/Manager/AddOrder',
    contentType: 'application/json',
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify({
        newOrder: $('#SaveOrder').serializeObject(),
        resultArray: [
            { orderListProp1: 'value1', orderListProp2: 'value2' },
            { orderListProp1: 'value3', orderListProp2: 'value4' },
            { orderListProp1: 'value5', orderListProp2: 'value6' }
        ]
    }),
    contentType: 'application/json; charset=utf-8'
});

ResultArray的内容是什么?这是MVC3吗?ResultArray的内容是什么?这是MVC 3吗?@user568262,在我的回答中,我建议使用视图模型(
MyViewModel
),它将聚合
NewOrder
ResultArray
属性,并将它们作为单个JSON对象发送。您可以使用
form.serialize()
分配
NewOrder
属性,并保留
ResultArray
属性,如我的示例所示。这对你不起作用吗?我尝试了伪值,比如{ClientFIO:'Hello'},它对表单值起作用,并绑定到“Order NewOrder”。现在只需要找到表单->json数据序列化的好插件,它应该可以工作了。我希望:)@user568262,请看看我的更新中建议的技术是否适合你。是的,我在5分钟前发现了这个连载,并试图让它工作,但你的例子没有给任何错误的机会。这太可怕了,你是我的导师:)一切都很完美。对不起,在你的第一篇文章中没有仔细观看。只是一个小问题。AddOrder Action=\@user568262中的RedirectToAction(“Index”)不起作用,在我的回答中,我建议使用一个视图模型(
MyViewModel
),它将聚合
NewOrder
ResultArray
属性,并将它们作为单个JSON对象发送。您可以使用
form.serialize()
分配
NewOrder
属性,并保留
ResultArray
属性,如我的示例所示。这对你不起作用吗?我尝试了伪值,比如{ClientFIO:'Hello'},它对表单值起作用,并绑定到“Order NewOrder”。现在只需要找到表单->json数据序列化的好插件,它应该可以工作了。我希望:)@user568262,请看看我的更新中建议的技术是否适合你。是的,我在5分钟前发现了这个连载,并试图让它工作,但你的例子没有给任何错误的机会。这太可怕了,你是我的导师:)一切都很完美。对不起,在你的第一篇文章中没有仔细观看。只是一个小问题。AddOrder操作中的重定向操作(“索引”)无效=\