2010-03-08 57 views
1

當我嘗試後與一個JavaScript數據對象執行以下操作:模型綁定到一個列表<>過賬的JavaScript數據對象

$.post(frm.attr("action"), data, function(res) 
{ 
    // do some stuff 
}, "json"); 

其中「數據」拍攝的結構

data 
- panelId 
- siteId 
- ConfiguredFactsheetId // this is an array of CheckBox ids that correspond to ConfiguredFactsheets 
    - 123 
    - 234 
    - 345 

由此,兩個站點&面板都被正確實例化,其中&與其數據綁定,但List對象爲空。

public JsonResult Edit(Site site, Panel panel, List<ConfiguredFactsheet> configuredFactsheets) 
{ 
    // do stuff 
    return Json(success); 
} 

現在,我意識到我的'data'對象的ConfiguredFactsheetId屬性只是一個id值的數組。我是否需要指定每個值都與我的ConfiguredFactsheet對象的configuredFactsheetId屬性相對應?如果是這樣,我的數據對象將採用這樣的形式similart到

data 
- panelId 
- siteId 
- ConfiguredFactsheet // this is an array of CheckBox ids that correspond to ConfiguredFactsheets 
    - ConfiguredFactsheetId:123 
    - ConfiguredFactsheetId:234 
    - ConfiguredFactsheetId:345 

但是這顯然是行不通的,因爲我每次添加一個新的ConfiguredFactsheetId對象時,它只會覆蓋前一個。

我知道我能做到這一點,如果我建的形式

"&ConfiguredFactsheet[i].configuredFactsheetId = " + configuredFactsheetId; 

,但我想包含在一個單一的數據對象

任何建議的所有內容的查詢字符串?我需要更清楚地解釋任何東西(可能是一切!)嗎?

感謝

戴夫

+0

據我所知,如果你有一個名爲'ConfiguredFactsheet'一個數組,你應該有參數名「configuredFactsheet'。如果它存儲了整數列表,它可能是'IEnumerable '。因此,使用'public JsonResult Edit(IEnumerable configuredFactsheet)'。 panelId如何轉換爲面板?你有你自己的ModelBinder嗎? – LukLed 2010-03-08 16:56:47

回答

1

最終,這個工作:

var valuesArray = objCheckBoxes.map(function() 
{ 
    return $.getAttributes($(this)); 
}); 

var obj = new Array(); 
$.each(valuesArray, function(item) { obj.push($(this)[0]); }); 

$.each(obj, function(i) 
{ 
    // basically I take the rule where you need to append 
    // the index to the type, and I apply it here. 
    data["configuredFactsheets[" + i + "].configuredFactsheetId"] = $(this).attr("configuredFactsheetId"); 
}); 

注:閱讀$.getAttributes

0

另一種方法是要發佈:

?myValues=1&myValues=2&myValues=3 

並接受它作爲一個IEnumerable

public ActionResult DoSomething(IEnumerable<int> myValues) { 
    ...