2010-07-05 59 views
0

我卡住了!模型綁定到IDictionary <int,IList <int>>在ASP.Net MVC 2.0

請幫助我。

我有以下視圖模型屬性在我的MVC 2.0項目......

public IDictionary<int, IList<int>> SelectedErrorsErrorType { get; set; } 

基本上我有下一個或多個選項卡複選框組。這些標籤就像英文,德文,法文等。我想使用tabId作爲詞典鍵,並選擇複選框ID作爲詞典值(整數列表)。我似乎無法像這樣綁定我的模型。我以前發現在映射到字典密鑰的stackoverflow上,我們需要隱藏字段,例如

<input type="hidden" name="<%: String.Format("SelectedErrorsErrorType[{0}].Key", index) %>"value="<%: item.LanguageId %>" /> 

這很好!但是,如何將Value字段映射到我的密鑰。

任何幫助將不勝感激。

謝謝。

回答

0

好像這裏是解決方案:ASP.NET MVC - Model binding a set of dynamically generated checkboxes - how to

但你也可以使用

public class ErrorType 
{ 
    public int Key { get; set; } 
    public int Value { get; set; } 
} 

public class ViewModel 
{ 
    public IList<ErrorType> PostedValues { get; set; } 

    public IDictionary<int, IList<int>> SelectedErrorsErrorType 
    { 
    get { return PostedValues.GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList()); 
    set { /* backward if you need it */ } 
    } 
} 

並綁定到PostedValues。

+0

嘿感謝你的努力的好友!我也會嘗試你的解決方案:) – user383698 2010-07-05 14:00:19

1

這很簡單..不知道爲什麼我不能弄清楚這一點。

下面是它的工作對我來說...

<input type="hidden" name="<%: String.Format("SelectedErrorsErrorType[{0}].Key", index) %>" 
                  value="<%: item.LanguageId %>" /> 

<%: Html.CheckBox(String.Format("SelectedErrorsErrorType[{0}].Value[{1}]", index, counter), false, new { @value = errorType.ErrorTypeId })%> 
+0

你需要把這個在你的文章不是一個新的職位! – RobertPitt 2010-07-05 13:59:47

+0

Ohhkk ...下一次會保重,哥們! – user383698 2010-07-07 10:49:14

相關問題