Jquery发布到带有字典参数的Action

Bil*_*nar 6 javascript asp.net-mvc jquery automapping asp.net-mvc-2

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

limiter[0].Key
limiter[0].Value
Run Code Online (Sandbox Code Playgroud)

以便它在动作中映射

public ActionResult SomeAction(Dictionary<Guid, string> dictionary) { }
Run Code Online (Sandbox Code Playgroud)

但是,这个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);
       }
)
Run Code Online (Sandbox Code Playgroud)

在用firebug检查时产生这个:

limiter[0][Key] = someKey // Guid Value
limiter[0][Value] = someValue
Run Code Online (Sandbox Code Playgroud)

这是在jq 1.4.2.我似乎记得你需要设置一些标志来在jQ中以不同的方式渲染json.这会响铃吗?

Dar*_*rov 5

尝试这样:

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

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

public ActionResult SomeAction(Dictionary<Guid, string> dictionary, string otherPostData) 
{ 
    ...
}
Run Code Online (Sandbox Code Playgroud)


小智 5

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)
{
}
Run Code Online (Sandbox Code Playgroud)

只需将您的 obj 作为 dataType: "json" 发送