gpt4 book ai didi

asp.net-mvc - ASP.NET MVC : How to display strongly typed view model, 包含项目列表,其中还包含项目列表?

转载 作者:行者123 更新时间:2023-12-04 06:56:04 25 4
gpt4 key购买 nike

我正在使用 ASP.NET MVC 构建一个应用程序,我想使用一个强类型 View 模型,其中包含一个名为 items 的 List ,其中包含一个 id int 和 itemName 字符串。 View 模型还包含一个名为 people 的 List ,而 Person 类包含一个 List

我想显示信息的方式是作为一个表格,每行有一列人名,然后有 n 个包含复选框的列,每个 List 一个,并根据 Person 的 List 是否检查 (称为项目)包含项目的 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.. 错误消息。

任何人都可以帮忙吗?

最佳答案

我自己解决了这个问题,这总是比得到答案更有值(value)......

这是我犯的一个愚蠢的错误:

我变了

<% foreach( var p in Model.people ) { %>


<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>

<% for( int p = 0; a<Model.people.Count; p++){ %>

<% var person = Model.people[p]; %>

然后在创建复选框时使用它:
<% if( person.items.Contains(i.id) ) { %>

<%= Html.CheckBox( "vm.people[" + p + "].items[" + i.id + "]", true ) %>

<% } else { %>

<%= Html.CheckBox( "vm.people[" + p + "].items[" + i.id + "]", false ) %>

<% } %>

关于asp.net-mvc - ASP.NET MVC : How to display strongly typed view model, 包含项目列表,其中还包含项目列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2559727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com