2
我正在構建一個使用ASP.NET MVC的應用程序,我希望使用一個強類型視圖模型,其中包含一個名爲<的項目>調用了包含id int和itemName字符串的項目。視圖模型還包含一個名爲<的人>調用人,而Person類包含一個列表<int>。ASP.NET MVC:如何顯示強類型視圖模型,其中包含項目列表,其中還包含項目列表?
我想要顯示信息的方式是以表格形式,每行有一列人名,然後n列包含複選框,列表<的每一列都有一個項目>,並根據是否人名單<int>(被稱爲物品)包含物品的ID。
我的顯示器工作正常,但我努力瞭解如何命名這些項目,以便發佈的方法可以讀取數據。
這是我在BeginForm:
<table cellpadding="20">
<thead>
<th>Person name</th>
<!-- for each of the items, create a column with the item name -->
<% foreach(var i in Model.items) { %>
<th><%= Html.Encode(i.itemName) %></th>
<% } %>
</thead>
<% foreach(var p in Model.people) { %>
<tr>
<td><%= Html.Encode(p.name) %></td>
<!-- for each item, create a column with a checkbox -->
<% foreach(var i in Model.items) { %>
<td>
<% if(p.items.Contains(i.id)) { %>
<!-- vm is the name of the view model passed to the view -->
<%= Html.CheckBox("vm.people[" + p.id + "].items[" + i.id + "]", true) %>
<% } else { %>
<%= Html.CheckBox("vm.people[" + p.id + "].items[" + i.id + "]", false) %>
<% } %>
</td>
<% } %>
</tr>
<% } %>
</table>
而這種代碼完全顯示的信息。然而,當我點擊提交時,我得到一個Object Reference Not Set ..錯誤信息。
任何人都可以幫忙嗎?
您可以編輯您的答案並將您的asp代碼放入'代碼'區塊; ) 獎勵:MVCContrib項目中有一組MVC助手,可以使用lambdas而不是魔術字符串。希望它能幫助你 – SDReyes 2010-04-01 13:23:54