javascript Jquery 使用字典参数发布到操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6984646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:35:34  来源:igfitidea点击:

Jquery post to Action with Dictionary Parameter

javascriptjqueryasp.net-mvcasp.net-mvc-2automapping

提问by Bill Bonar

I am feeling dejavu, but I cannot find the answer to this: I have an array of objects that needs to look like this when inspecting a jQ $.post call:

我感觉很像,但我找不到答案:在检查 jQ $.post 调用时,我有一组对象需要看起来像这样:

limiter[0].Key
limiter[0].Value

so that it is mapped in the action

以便将其映射到操作中

public ActionResult SomeAction(Dictionary<Guid, string> dictionary) { }

However, this javascript:

但是,这个javascript:

// Some Guid and Some Value
var param = [ { 'Key' : '00000000-0000-00000-000000', 'Value': 'someValue' } ];

$.post('/SomeController/SomeAction/',
       {
       dictionary: limiter,
       otherPostData: data
       },
       function(data) {
          callback(data);
       }
)

produces this when inspecting it in firebug:

在萤火虫中检查它时会产生这个:

limiter[0][Key] = someKey // Guid Value
limiter[0][Value] = someValue

This is in jq 1.4.2. I seem to remember some flag you need to set to render json a different way in jQ. Does this ring any bells?

这是在 jq 1.4.2 中。我似乎记得你需要设置一些标志来在 jQ 中以不同的方式呈现 json。这是否敲响了警钟?

回答by Darin Dimitrov

Try like this:

像这样尝试:

var param = {
    '[0].Key': '28fff84a-76ad-4bf6-bc6d-aea4a30869b1', 
    '[0].Value': 'someValue 1',

    '[1].Key': 'd29fdac3-5879-439d-80a8-10fe4bb97b18', 
    '[1].Value': 'someValue 2',

    'otherPostData': 'some other data'
};

$.ajax({
    url: '/Home/SomeAction/',
    type: 'POST',
    data: param,
    success: function (data) {
        alert(data);
    }
});

should map to the following controller action:

应该映射到以下控制器操作:

public ActionResult SomeAction(Dictionary<Guid, string> dictionary, string otherPostData) 
{ 
    ...
}

回答by Alex Kvitchastiy

var dict = {}
dict["key1"] = 1
dict["key2"] = 2
dict["key3"] = 3

$.ajax({
        type: "POST",
        url: "/File/Import",
        data: dict,
        dataType: "json"
});

public void Import(Dictionary<string, int?> dict)
{
}

just send your obj as dataType: "json"

只需将您的 obj 作为数据类型发送:“json”

回答by ipr101

You can use this flag -

您可以使用此标志 -

jQuery.ajaxSetting.traditional = true;

To get jQuery to post the data in a different format to the one you are seeing. See this question for further info -

让 jQuery 以与您所看到的格式不同的格式发布数据。请参阅此问题以获取更多信息 -

Passing arrays in ajax call using jQuery 1.4

使用 jQuery 1.4 在 ajax 调用中传递数组

回答by ShankarSangoli

You will see the post param as limiter[0][Key]because jquery serializes the json data before it posts it. This is very well interpreted by the controller action and you get the required input in the action.

您将看到 post 参数,limiter[0][Key]因为 jquery在发布之前序列化了 json 数据。控制器动作很好地解释了这一点,您可以在动作中获得所需的输入。

回答by RealityDysfunction

You can also use a list of objects and the result will be the same as what you wanted. This is a nice example.

您还可以使用对象列表,结果将与您想要的相同。这是一个很好的例子。

http://www.mikesdotnetting.com/Article/96/Handling-JSON-Arrays-returned-from-ASP.NET-Web-Services-with-jQuery

http://www.mikesdotnetting.com/Article/96/Handling-JSON-Arrays-returned-from-ASP.NET-Web-Services-with-jQuery