如何通过循环使用ASP.NET MVC5连续显示3个项目 [英] How to display 3 items in a row through looping with ASP.NET MVC5
问题描述
如何通过使用Asp.Net循环连续显示3个项目
How to display 3 items in a row through looping with Asp.Net
@model IEnumerable<CMSFC.Models.ContentHtml>
我尝试了这个但是没有用。这是我遇到的错误:
I tried this but doesn't work. This is the error that I encounter:
Server Error in '/' Application.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: The foreach block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
Source Error:
Line 35: @{var counter = 0; }
Line 36:
Line 37: @foreach (var item in Model)
Line 38: {
Line 39:
Source File: /Views/ContentHtml/Tabelar.cshtml Line: 37
我想连续显示3个Id-s。
我尝试过:
I want to show 3 Id-s at a row.
What I have tried:
@model IEnumerable<CMSFC.Models.ContentHtml>
<table>
@{var counter = 1; }
<tr>
@foreach (var item in Model){
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
if(counter % 3 == 0){
@:
</tr>
<tr>
}
counter++;
}
</tr>
</table>
推荐答案
您还没有分享您遇到的错误,但我有一个非常好的主意它与此有关:
You have not shared the error that you are encountering but I have a pretty good idea it has something to do with this:
@model IEnumerable<CMSFC.Models.ContentHtml>
你不能迭代一个对象,只是一个对象的集合。所以上面的行应该是:
you can not iterate an object, only a collection of object. So the above line should be:
@model IEnumerable<CMSFC.Models>
更新:您的剃刀代码中有语法错误。该错误表明解析器看到缺少语法 - 您应该在此行代码中看到一条红色波浪线:
UPDATE: You have a Syntax error in your razor code. The error is indicating that the parser sees missing syntax - you should see a red squiggly line on this line of code:
@Html.DisplayFor(modelItem => item.Id)
即使您在HTML页面中,剃刀语言仍然具有与C#相同的语法规则。因此,从上面这一行可以很容易地看出,这行代码没有以'; '结尾。所以它应该是:
Even though you are in an HTML page, the razor language still has the same syntax rules as C#. So from the above line, it is pretty easy to see that this line of code is not terminated with a ';'. So it should be:
@Html.DisplayFor(modelItem => item.Id);
@:
</tr>
<tr>
这些行很可能是问题所在。要么它们都应该在同一行,要么每个标记都应该以为前缀:@:
。
Those lines are most likely the problem. Either they should all be on the same line, or each tag should be prefixed with @:
.
@model IEnumerable<CMSFC.Models.ContentHtml>
<table>
@{ var counter = 1; }
<tr>
@foreach (var item in Model){
<td>
@Html.DisplayFor(m => item.Id)
</td>
if(counter % 3 == 0){
@:</tr>
@:<tr>
}
counter++;
}
</tr>
</table>
asp.net mvc 3 - Razor不理解未关闭的html标签 - Stack Overflow [ ^ ]
或者,您可以使用LINQ对项目进行分组,这可能会使标记更简单:
asp.net mvc 3 - Razor doesn't understand unclosed html tags - Stack Overflow[^]
Alternatively, you could use LINQ to group the items, which might make the markup simpler:
<table>
@foreach (var group in Model
.Select((item, index) => new { item, key = index / 3 })
.GroupBy(p => p.key, p => p.item))
{
<tr>
@foreach (var item in group)
{
<td>
@Html.DisplayFor(m => item.Id)
</td>
}
</tr>
}
</table>
永远不要使用DispalyFor()来显示Ienumarable项目中的项目。或者你从模型元数据迭代。
Never use DispalyFor() to display item in Ienumarable items. or you have iterate from model metadata.
@foreach (var item in Model){
<tr>
@if(count % 3 == 0)
{
<td>
@:
</td>
}
else
{
<td>
@item.id
</td>
}
</tr>
}
这篇关于如何通过循环使用ASP.NET MVC5连续显示3个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!