【发布时间】:2011-02-03 07:43:36
【问题描述】:
我正在使用 ASP.NET MVC 构建一个应用程序,我想使用强类型视图模型,其中包含一个名为 items 的 List
我想要显示信息的方式是作为一个表格,每一行都有一个人名列,然后是 n 个包含复选框的列,每个 List
我的显示器工作正常,但我很难理解如何命名项目,以便发布的方法可以读取数据。
这是我在 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>
这段代码完美地显示了信息。但是,当我单击提交时,我收到未设置对象引用.. 错误消息。
有人可以帮忙吗?
【问题讨论】:
标签: asp.net-mvc