如何在剃刀视图中将IEnumerable数据通过ASP.NET MVC索引并实现并排展示?

2026-03-30 11:511阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计450个文字,预计阅读时间需要2分钟。

如何在剃刀视图中将IEnumerable数据通过ASP.NET MVC索引并实现并排展示?

我在开发一个Asp.net MVC应用程序时,遇到了一个场景,需要在两列中显示内容(即并列排列)。我通过Google搜索并在这里找到了解决方案。尽管我尝试过,但效果不佳。我尝试过这种形式:`@model IEnumerable.MvcApplication`。

我正在开发一个 Asp.net MVC应用程序,遇到了一个场景,我必须在两列中显示内容(即并排)我用Google搜索并在这里找到了解决方案.我试过但徒劳无功.
我试过这种方式

如何在剃刀视图中将IEnumerable数据通过ASP.NET MVC索引并实现并排展示?

@model IEnumerable<MvcApplication1.tblTest> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <table> <tr> <th> testId </th> <th> testName </th> <th> testDescription </th> <th></th> </tr> @for (var i = 0; i < Model.Count(); i+=2 ) { <tr> <td> @Model[i].testId </td> </tr> } </table>

但是我收到了编译错误 –
编译错误

描述:编译服务此请求所需的资源时发生错误.请查看以下特定错误详细信息并相应地修改源代码.

Compiler Error Message: CS0021: Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<MvcApplication1.tblTest>' Source Error: Line 27: <tr> Line 28: <td> Line 29: @Model[i].testId Line 30: </td> Line 31:

有谁可以帮我解决这个问题?

简单地说,您无法索引可枚举的内容.它被设计为按顺序一次吐出一个项目,而不是堆栈中任何位置的特定项目.最简单的解决方案是将其转换为List:

@{ var tblTestList = Model.ToList(); for (var i = 0; i < tblTestList.Count(); i+=2 ) { <tr> <td> @tblTestList[i].testId </td> </tr> } }

甚至更简单:

@model List<MvcApplication1.tblTest>

本文共计450个文字,预计阅读时间需要2分钟。

如何在剃刀视图中将IEnumerable数据通过ASP.NET MVC索引并实现并排展示?

我在开发一个Asp.net MVC应用程序时,遇到了一个场景,需要在两列中显示内容(即并列排列)。我通过Google搜索并在这里找到了解决方案。尽管我尝试过,但效果不佳。我尝试过这种形式:`@model IEnumerable.MvcApplication`。

我正在开发一个 Asp.net MVC应用程序,遇到了一个场景,我必须在两列中显示内容(即并排)我用Google搜索并在这里找到了解决方案.我试过但徒劳无功.
我试过这种方式

如何在剃刀视图中将IEnumerable数据通过ASP.NET MVC索引并实现并排展示?

@model IEnumerable<MvcApplication1.tblTest> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <table> <tr> <th> testId </th> <th> testName </th> <th> testDescription </th> <th></th> </tr> @for (var i = 0; i < Model.Count(); i+=2 ) { <tr> <td> @Model[i].testId </td> </tr> } </table>

但是我收到了编译错误 –
编译错误

描述:编译服务此请求所需的资源时发生错误.请查看以下特定错误详细信息并相应地修改源代码.

Compiler Error Message: CS0021: Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<MvcApplication1.tblTest>' Source Error: Line 27: <tr> Line 28: <td> Line 29: @Model[i].testId Line 30: </td> Line 31:

有谁可以帮我解决这个问题?

简单地说,您无法索引可枚举的内容.它被设计为按顺序一次吐出一个项目,而不是堆栈中任何位置的特定项目.最简单的解决方案是将其转换为List:

@{ var tblTestList = Model.ToList(); for (var i = 0; i < tblTestList.Count(); i+=2 ) { <tr> <td> @tblTestList[i].testId </td> </tr> } }

甚至更简单:

@model List<MvcApplication1.tblTest>