在我的界面我的文本框列表中,這樣的事情: 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的在我的控制器的動作?
非常感謝您的幫助
嘗試'公衆的ActionResult保存(INT?ID,字典 CBTS)' –
2010-06-28 00:53:04
謝謝,這是更好! – Mose 2010-07-06 09:10:28