How can I read loops in mvc3 in @foreach

How can I close <tr>and open <tr>after three iterations of a loop? I have MVC 3 in .NET 4.0. How can I calculate loop iterations in MVC 3?

Current Code:

@foreach (var articleOnFirstPage in Model.ArticlesOnFirstSite)
{

<tr>
    <td><div class="productsFrame"></div></td>
</tr>

}

I want to get the following:

<tr>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
    </tr>
<tr>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
    </tr>
<tr>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
    </tr>
+5
source share
4 answers

You can do the following pornography in your opinion:

@model IEnumerable<Foo>
<table>
@foreach (var item in from i in Model.Select((value, index) => new { value, index }) group i.value by i.index / 3 into g select g)
{
    <tr>
        @foreach (var x in item)
        {
            <td><div class="productsFrame">@x.SomeProperty</div></td>
        }
    </tr>
}
</table>

, , , , . , , , , 3. . IEnumerable<Foo> . Pass IEnumerable<MyViewModel>, , MyViewModel , , , foreach- . , :

<table>
    @HtmlDisplayForModel()
</table>

, ?


, .

ASP.NET MVC , ( : ):

public class ItemViewModel
{
    public string Title { get; set; }
}

public class MyViewModel
{
    public IEnumerable<ItemViewModel> Items { get; set; }
}

, :

public class HomeController : Controller
{
    public ActionResult Index()
    {
        // Obviously in a real world application the data is your domain model
        // and comes from a repository or a service layer depending on the complexity
        // of your application. I am hardcoding it here for the 
        // purposes of the demonstration
        var data = Enumerable.Range(1, 30).Select(x => new { Title = "title " + x });

        var model =
            from i in data.Select((value, index) => new { value, index })
            group i.value by i.index / 3 into g
            select new MyViewModel
            {
                Items = g.Select(x => new ItemViewModel { Title = x.Title })
            };

        return View(model);
    }
}

, , (~/Views/Home/Index.cshtml):

@model IEnumerable<MyViewModel>
<table>
    @Html.DisplayForModel()
</table>

~/Views/Home/DisplateTemplates/MyViewModel.cshtml:

@model MyViewModel
<tr>
    @Html.DisplayFor(x => x.Items)
</tr>

, , ~/Views/Home/DisplateTemplates/ItemViewModel.cshtml:

@model ItemViewModel
<td>@Html.DisplayFor(x => x.Title)</td>

. , , .

, , AutoMapper, , , :

public ActionResult Index()
{
    IEnumerable<DomainModel> data = ...
    var viewModel = Mapper.Map<IEnumerable<DomainModel>, IEnumerable<MyViewModel>>(data);
    return View(viewModel);
}

:

[AutoMap(typeof(IEnumerable<DomainModel>), typeof(IEnumerable<MyViewModel>))]
public ActionResult Index()
{
    IEnumerable<DomainModel> data = ...
    return View(data);
}

.

+5

, , - Phil Haack foreach

,

<ol>
@Model.Each(@<li>Item @item.Index of @(Model.Count() - 1): @item.Item.Title</li>)
</ol>

, , :

@Model.ArticlesOnFirstSite.Each(@<td><div class="productsFrame"></div></td>@(@item.Index % 3 == 0 ? "</tr><tr>" : ""))
+5

- .

@{int i = 0;}
@foreach (var articleOnFirstPage in Model.ArticlesOnFirstSite)
{
    @if ((i++ % 3) == 0) {
        @if (i != 1) {
            @:</tr>
        }
        @:<tr>
    }

    @:<td><div class="productsFrame"></div></td>
}

@if (i != 0) {
    @:</tr>
}

.

, : , 3.

About how to use the template model-view-controller, which you can see on the Internet. http://msdn.microsoft.com/en-us/library/gg416514(v=vs.98).aspx is a good start.

+3
source

Like everyone else, the best and finest solution is probably grouping in the controller, but this can do the job:

@for (int i = 0; i < Model.ArticlesOnFirstSite.Count; i += 3)
{
    <tr>
        @foreach (Article article in Model.ArticlesOnFirstSite.Skip(i).Take(3))
        {
            <td>@article.Title</td>
        }
    </tr>
}
0
source

All Articles