Jesse
Jesse

Reputation: 47

Ordering Html.DisplayFor in a foreach loop

I want to sort my foreach loop and I'm using Html.DisplayFor(). I have 3 kind of surfboards with a category / color.

The code:

@foreach (var item in Model)
{
    <div style="width:25%;" class="post_grid">
        <div class="postImage">
            <a href="#">
                <center><img style="height:428px" src="@Html.DisplayFor(modelItem => item.Afbeelding)"/></center>
            </a>
        </div>
        <h3>@Html.DisplayFor(modelItem => item.Boardnaam)</h3>
        <h4 style="text-align:right;">&euro; @Html.DisplayFor(modelItem => item.Prijs)</h4>
        <h4>@Html.DisplayFor(modelItem => item.Merk)</h4>
        <div class="Informatie">
            <H5>@Html.DisplayFor(modelItem => item.Beschrijving)</H5>
        </div>
        <div class="button">
            @Html.ActionLink("More Information", "Details", new { id=item.ID })
        </div>
    </div>
}

So I want my foreach loop to display all items with the color thats in my model called White. For example: If item.Kleur = "White" -> Show the Html.DisplayFor() items.

Can someone help me with this?

Upvotes: 0

Views: 740

Answers (1)

Marcin Ciesla
Marcin Ciesla

Reputation: 84

If you want to iterate only through white items, you can use

@foreach (var item in Model.Where(e => e.Kleur == "White"))
    {
        <div style="width:25%;" class="post_grid">
            <div class="postImage">
                <a href="#">
                    <center><img style="height:428px" src="@Html.DisplayFor(modelItem => item.Afbeelding)"/></center>
                </a>
            </div>
            <h3>@Html.DisplayFor(modelItem => item.Boardnaam)</h3>
            <h4 style="text-align:right;">&euro; @Html.DisplayFor(modelItem => item.Prijs)</h4>
            <h4>@Html.DisplayFor(modelItem => item.Merk)</h4>
            <div class="Informatie">
                <H5>@Html.DisplayFor(modelItem => item.Beschrijving)</H5>
            </div>
            <div class="button">
                @Html.ActionLink("More Information", "Details", new { id=item.ID })
            </div>
        </div>
    }

or if you want to display it if condition is meet (ie for different items different setup) you can use

    @foreach (var item in Model)
      {
       if (item.Kleur == "White")
         {
            <div style="width:25%;" class="post_grid">
              <div class="postImage">
                <a href="#">
                    <center><img style="height:428px" src="@Html.DisplayFor(modelItem => item.Afbeelding)"/></center>
                </a>
            </div>
            <h3>@Html.DisplayFor(modelItem => item.Boardnaam)</h3>
            <h4 style="text-align:right;">&euro; @Html.DisplayFor(modelItem => item.Prijs)</h4>
            <h4>@Html.DisplayFor(modelItem => item.Merk)</h4>
            <div class="Informatie">
                <H5>@Html.DisplayFor(modelItem => item.Beschrijving)</H5>
            </div>
            <div class="button">
                @Html.ActionLink("More Information", "Details", new { id=item.ID })
            </div>
        </div>
      }
    }

Upvotes: 1

Related Questions