Display three columns per row in MVC cshtml

前端 未结 2 911
[愿得一人]
[愿得一人] 2020-12-19 16:10

What I have currently is the below which works fine but now it shows my records in a long list, what i want to do is show three(3) records per row. I tried putting a for loo

2条回答
  •  死守一世寂寞
    2020-12-19 17:03

    Use for block and print or based on the value of i. If it's the first index (i equals 0) or it's the fourth, seventh, ... (3n+1)th index (i % 3 equals 0), then print before . If it's the last index (i equals Model.Clients.Count - 1) or it's the third, sixth, ... (3n)th index (i % 3 equals 2), then print after . The below code should give what you want.

    @for (int i = 0; i < Model.Clients.Count; i++)
    {
        if (i == 0 || i % 3 == 0)
        {
            
        }
        
            
    @Html.Hidden("ClientID", Model.Clients[i].ClientID) @Html.Label(Model.Clients[i].ClientName)
    if (i % 3 == 2 || i == Model.Clients.Count - 1) { } }

提交回复
热议问题