Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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>
Posted
Updated 21-Aug-17 5:27am
v3

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)

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);
 
Share this answer
 
v3
Comments
ddgjgj 21-Aug-17 5:48am    
can you provide the full code
Graeme_Grant 21-Aug-17 6:08am    
I don't understand the request.
ddgjgj 21-Aug-17 6:24am    
This is the code:

@model IList<cmsfc.models.contenthtml>


@{var counter = 0; }

@foreach (var item in Model)
{

if (counter % 3 == 0)
{
@:


}
@counter++;
}


@Html.DisplayFor(modelItem => item.Id)





And thats the error:

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
Graeme_Grant 21-Aug-17 7:22am    
This was the *KEY* information missing from your question. Without this, we had no chance of knowing what was your problem. Do you understand???

I moved the error back into the question for you and will update my solution with the UPDATED answer.
Richard Deeming 21-Aug-17 11:11am    
Razor doesn't need a terminating semi-colon for inline statements like @Html.DisplayFor(...). If you include it, it will be included in the HTML output. :)

C# Razor Syntax Quick Reference | You’ve Been Haacked[^]
Quote:
@:
</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 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>
 
Share this answer
 
v2
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>
}
 
Share this answer
 
Comments
ddgjgj 21-Aug-17 4:49am    
It says this :

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: ":" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.


Source Error:


Line 12: {
Line 13:
Line 14: @:
Line 15:
Line 16: }
i think u still newbie on C#,

if(counter % 3 == 0){
                @:
            </tr>
            <tr>
             }


Not need C++ count inside foreach loop and not match with beginning of

follow my sample u you could,
 
Share this answer
 
Comments
ddgjgj 21-Aug-17 4:55am    
Not working
Richard Deeming 21-Aug-17 11:07am    
If it's not working, why have you accepted the solution?

You should only mark the solution as "accepted" if it solves your problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900