2011-08-08 42 views
6

我感覺DEJAVU,但我無法找到這個問題的答案: 我有對象的數組需要像這樣檢查一個JQ $。員額打電話時:jQuery的崗位操作與字典參數

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

,使其在動作映射

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

然而,此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); 
     } 
) 

在螢火檢查時它產生這樣的:

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

這是JQ 1.4.2。我似乎記得你需要設置一個標誌來在jQ中以不同的方式呈現json。這鈴響嗎?

回答

3

嘗試這樣的:

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); 
    } 
}); 

應該映射到下列控制器動作:

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

你忘記了參數名稱,應該是dictionary [0] .key,但這對我有用。我仍然困惑爲什麼其他方式沒有,但感謝您的幫助。 –

1

您可以使用此標誌 -

jQuery.ajaxSetting.traditional = true; 

爲了讓jQuery來以不同的格式,以您所看到的一個帖子中的數據。看到這個問題的進一步信息 -

Passing arrays in ajax call using jQuery 1.4

+0

可惜'傳統flag'沒有幫助。當在螢火蟲 –

0

您將看到後PARAM作爲limiter[0][Key]因爲jQuery序列化JSON數據之前其發佈。這通過控制器操作很好地解釋,並且您在操作中獲得所需的輸入。

+0

中查看時使用它['Object:Object]'我發佈這個,因爲它沒有被映射,但我會更新我的代碼以獲得更完整性,看看是否有人可以幫助 –

0
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) 
{ 
} 

只是給你的OBJ爲數據類型: 「JSON」