2010-05-25 23 views
0

在我的界面我的文本框列表中,這樣的事情: http://screencast.com/t/YjIxNjUyNmUASP.Net MVC 2 - ModelBinding和字典<int, int>

文本框的數量尚不清楚,因爲他們每個人都與模板關聯。 在我的頁面中,目標是將一個數字與某些模板關聯起來。

下面是一個簡單的HTML代碼:

<% // loop on the templates 
    foreach(ITemplate template in templates) 
    { 
     // get the content from the input dictionary 
     int val; 
     content.TryGetValue(template.Id, out val); 
     // convert it as a string 
     string value = ((val > 0) ? val.ToString() : string.Empty); 

     // compute the element name/id (for dictionary binding) 
     string id = ?????????? 
     string name = ?????????????? 
%> 
     <label for="<%= name %>"><%= template.Name %></label> 
     <input type="text" id="<%= id %>" name="<%= name %>" value="<%= value %>" /> 
     <br /> 
<% } 
%> 

我期待什麼,在我的控制器,是爲了得到一個IDictionary的,其中第一個int是模板ID,另一種是由用戶給出的計數。

這裏是我想要的東西:

public ActionResult Save(int? id, Dictionary<int, int> countByTemplate) 

我嘗試了很多東西,但沒有奏效。我嘗試閱讀源代碼,但這是一個迷宮,我正在嘗試獲取有關模型綁定的信息。

問題:

  • 有上modelbinding如何工作的的ressource? 我想要詳盡無遺,我厭倦了討論給定具體示例的84093043博客。
  • 我怎麼能建立我的HTML,使用獲得的IDictionary(甚至IDictionary的在我的控制器的動作?

非常感謝您的幫助

回答

1

好吧...感謝列維我能得到一個解決方案。 不是清潔的,但它的工作原理。

的HTML應該這樣寫:

<% 
int counter = 0; 
// loop on the templates 
foreach(ITemplate template in templates) 
{ 
     // get the value as text 
     int val; 
     content.TryGetValue(template.Id, out val); 
     var value = ((val > 0) ? val.ToString() : string.Empty); 

     // compute the element name (for dictionary binding) 
     string id = "cbts_{0}".FormatMe(template.Id); 
     string dictKey = "cbts[{0}].Key".FormatMe(counter); 
     string dictValue = "cbts[{0}].Value".FormatMe(counter++); 
%> 
     <input type="hidden" name="<%= dictKey %>" value="<%= template.Id %>" /> 
     <input type="text" id="<%= id %>" name="<%= dictValue %>" value="<%= value %>" /> 
     <label for="<%= id %>"><%= template.Name %></label> 
     <br /> 
<% } 
%> 

我不得不添加一個隱藏字段來存儲值。 我引入了一個'假'計數器,只是以ASP.Net MVC想要的方式遍歷字典。 因此,當文本框爲空時,我得到了一個填充了我的值的字典和'0'。

出現了另一個問題:ModelState被認爲是無效的,因爲「需要一個值」。我不希望我的值是必需的,但查看模型綁定器代碼,我沒有找到一種方法來告訴活頁夾不需要一個值。

所以我在我的控制器欺騙的ModelState,消除所有的錯誤,像這樣:

public ActionResult Save(int? id, Dictionary<int, int> cbts) 
{ 
    // clear all errors from the modelstate 
    foreach(var value in this.ModelState.Values) 
     value.Errors.Clear(); 

嗯......我得到了有效的解決方案,但現在HTML是一種醜,反直覺的(使用循環一個非索引集合的索引??)。 我每次需要使用這種綁定來讓它一切正常。

因此,我現在將打開一個新的職位,使字典粘合劑更好的東西。 這就是:ASP.Net MVC 2 - better ModelBinding for Dictionary<int, int>

編輯 - 有一個清潔的解決方案,這要歸功於帕維爾Chuchuva。

在控制器代碼中,使用可爲空的int作爲字典的值。 添加一點點代碼,但更乾淨。

public ActionResult Save(int? id, Dictionary<int, int?> cbts) 
{ 
    // this is our final dictionary<int, int> 
    Dictionary<int, int> cbtsFinal = new Dictionary<int, int>(); 
    // loop on the dicitonary with nullable values 
    foreach(var key in cbts.Keys) 
    { 
     // if we have a value 
     if(cbts[key].HasValue) 
      // then put it in the final dictionary 
      cbtsFinal.Add(key, cbts[key].Value); 
    } 
+0

嘗試'公衆的ActionResult保存(INT?ID,字典 CBTS)' – 2010-06-28 00:53:04

+0

謝謝,這是更好! – Mose 2010-07-06 09:10:28