通过jQuery.Ajax发送多个项目MVC控制器 [英] Sending Multiple Items to MVC Controller via jQuery.Ajax

查看:152
本文介绍了通过jQuery.Ajax发送多个项目MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有啊正在序列化使用类似jQuery的一种形式,它往往会映射JSON键和值对您发布到控制器动作一个对象的属性。所以:

jQuery的:

 函数PostForm(){
    $阿贾克斯({
        网址:/首页/ TestMVC
        键入:POST,
        数据类型:应用/ JSON
        数据:$('#形式')序列化()。
        完成:callFunction
        }
    });

presuming主要细节包含将具有参数的名称作为关键字它们应该映射到直接在物体元素:

动作:

 公共无效TestMVC(为MyObject OBJ)
{
//的OBJ现在应该包含从序列化表单中的数据
}

POST:

 名称:鲍勃
年龄:999
性别:未知

有谁知道这是如何工作?它打破每次我通过表格时, 和任何其他数据到控制器。

我想发送的数据的内容,以及其中可能包含键/值对的任意数量和类型的到控制器的查询字符串。我可以在服务器上提取这些键/值对,因为我不能在方法签名为他们创建一个对象。然而,这未能按预期工作。

jQuery的:

 函数PostForm(){    $阿贾克斯({
        网址:/首页/ TestMVC
        键入:POST,
        数据类型:应用/ JSON
        数据:
        {
           OBJ:$('#形式')序列化()
           TheWeirdQueryString:$('。additionalParams')序列化()
        }
    });
};

动作:

 公共无效TestMVC(OBJ为MyObject,字符串TheWeirdQueryString)
{
//现在的OBJ不包含元素,它是NULL。而TheWeirdQueryString工作正常。
}

发表

 的OBJ:名称=鲍勃和放大器;年龄= 999安培;性别=未知
TheWeirdQueryString:参数1 = 1&安培;参数2 = 2

我想这是因为我实际上已经创建了一个JSON对象作为数据和属性设置为对象的名称。

有是其中出现在萤火虫在POST值的差异。当我单独发布对象,在POST值是对象/形式与其相应的值的所有的键。在第二个例子有两个简单的属性,这个名字我给他们,每个都包含一个查询字符串(富= 1&安培;酒吧= 2 )和MVC不能映射查询字符串到一个对象的成员(或因此它会出现)。

反正都去上班像它在第一时间做,而且要额外的数据发送到在行动第2个说法?我猜这是一个额外的属性,当jQuery不会形式的序列化添加到创建的所有现有的。

在POST其实我是想的是:

 名称:鲍勃
年龄:999
性别:未知
TheWeirdQueryString:参数1 = 1&安培;参数2 = 2


解决方案

在$阿贾克斯方法dataType参数是反应的类型(即你希望从服务器返回的数据的类型),而不是索取。试试这个:

 函数PostForm(){
    $阿贾克斯({
        网址:/首页/ TestMVC
        键入:POST,
        数据类型:应用/ JSON
        数据:$('#形式')序列化()+&放大器;。 + $('。additionalParams')。序列化()
    });
};

 函数PostForm(){
    $阿贾克斯({
        网址:/首页/ TestMVC+? + $('。additionalParams')。序列化()
        键入:POST,
        数据类型:应用/ JSON
        数据:$('#形式')序列化()。
    });
};

更新:

试试这个:

控制器:

 公共无效TestMVC(OBJ为MyObject,字符串[] TheWeirdQueryString)
{
}

客户:

 函数PostForm(){
    $阿贾克斯({
        网址:/首页/ TestMVC
        键入:POST,
        数据类型:应用/ JSON
        数据:$('#形式')序列化()+&放大器;。 + $('。additionalParams')。序列化()
    });
};

但在客户端的额外PARAMS必须采用以下格式:

<$p$p><$c$c>TheWeirdQueryString[0]=param1&TheWeirdQueryString[1]=param2&...&TheWeirdQueryString[n]=paramN

因此​​$('。additionalParams')元素必须有身份证和/或名属性,如:TheWeirdQueryString [1],TheWeirdQueryString [2] ... TheWeirdQueryString [N]

希望这有助于

If youa are serializing a form using something like jQuery, it will often map the JSON keys and values to the properties of a object on the Controller Action you are posting to. So:

jQuery:

function PostForm() {
    $.ajax({
        url: "/Home/TestMVC",
        type: "POST",
        dataType: "application/JSON",
        data:  $('#form').serialize(),
        complete: callFunction
        }
    });

Presuming main details contains elements which will have the name of the parameter as a key they should map to the object directly:

Action:

public void TestMVC(MyObject obj)
{
//Obj should now contain the data from the serialised form
}

POST:

Name: "Bob"
Age: "999"
Sex: "Unknown"

Does anyone know how this works? It's breaking every time I pass the form and any additional data to the controller.

I would like to send the contents of the data as well as a QueryString which could contain any number and types of key/value pairs to the controller. I can extract these key/value pairs on the server since I cannot create an object for them on the method signature. However, this fails to work as intended.

jQuery:

function PostForm() {

    $.ajax({
        url: "/Home/TestMVC",
        type: "POST",
        dataType: "application/JSON",
        data: 
        { 
           Obj: $('#form').serialize(),
           TheWeirdQueryString: $('.additionalParams').serialize(),
        }
    });
};

Action:

public void TestMVC(MyObject obj, String TheWeirdQueryString)
{
//Obj now does NOT contain the element, it is NULL. Whereas TheWeirdQueryString works fine. 
}

Post:

Obj: name=bob&age=999&sex="unknown"
TheWeirdQueryString: param1=1&param2=2

I think this is because I have actually created a JSON object as the Data and set the properties to the name of the object.

There is a difference in the POST values which appear in Firebug. When I post the object alone, the POST values are all the keys of the object/form with their corresponding values. In the second example there are two simple properties, The name I gave them, each containing a QueryString (Foo=1&Bar=2) and MVC cannot map a QueryString to the members of an object (or so it would appear).

Is there anyway at all to get to work like it does in the first instance, but also to send extra data to a 2nd argument on the Action? I am guessing it's to add an extra property to all the existing ones created when jquery does the serialisation of the form.

The POST I actually want is:

Name: "Bob"
Age: "999"
Sex: "Unknown"
TheWeirdQueryString: param1=1&param2=2

解决方案

dataType parameter in $.ajax method is the type of response (the type of data that you're expecting back from the server), not request. Try this instead:

function PostForm() {
    $.ajax({
        url: "/Home/TestMVC",
        type: "POST",
        dataType: "application/JSON",
        data: $('#form').serialize() + "&" + $('.additionalParams').serialize()
    });
};

or:

function PostForm() {
    $.ajax({
        url: "/Home/TestMVC" + "?" + $('.additionalParams').serialize(),
        type: "POST",
        dataType: "application/JSON",
        data: $('#form').serialize()
    });
};

UPDATED:

Try this:

Controller:

public void TestMVC(MyObject obj, String[] TheWeirdQueryString)
{
}

Client:

function PostForm() {
    $.ajax({
        url: "/Home/TestMVC",
        type: "POST",
        dataType: "application/JSON",
        data: $('#form').serialize() + "&" + $('.additionalParams').serialize()
    });
};

but on client side your additional params must be in the following format:

TheWeirdQueryString[0]=param1&TheWeirdQueryString[1]=param2&...&TheWeirdQueryString[n]=paramN

so $('.additionalParams') elements must have "id" and/or "name" attributes like: TheWeirdQueryString[1], TheWeirdQueryString[2] ... TheWeirdQueryString[N]

Hope this helps

这篇关于通过jQuery.Ajax发送多个项目MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆