2

I have a "td" which contains 9 pictures and i want to split them 3 by 3. So i want to get a view like this:

  • Pic1 Pic2 Pic3
  • Pic4 Pic5 Pic6
  • Pic7 Pic8 Pic9

Here is my View code.

@for (int k = 0; k < Model.Count; k++)
  {
  <td>
  <img src="(Model[k].Pic)" width="80" height="90" />
  </td>
  }

I am trying to split pics by this code:

@for (int k = 0; k < Model.Count; k++)
{
 <td>

 <div>
  <img src="(Model[k].Pic)" width="80" height="90" />

  </div>   
 @if (k % 3 == 0)
{
     @:<div></div>
}

     </td>
  }                                             

But i can't split pictures. It's an easy code but i failed. Where the problem is?

0

2 Answers 2

2

You can create a LINQ method to create partitions with a specified size:

public static class PartitionExtensions
{
    private static IEnumerable<IEnumerable<T>> ToSizedPartition<T>(IEnumerable<T> source, int size)
    {
        int currentPartitionCount = 0;
        T[] array = null;

        foreach (T item in source)
        {
            if (array == null)
            {
                array = new T[size];
            }

            array[currentPartitionCount] = item;
            currentPartitionCount++;

            if (currentPartitionCount == size)
            {
                yield return array;
                currentPartitionCount = 0;
                array = null;
            }
        }

        if (array != null)
        {
            Array.Resize(ref array, currentPartitionCount);
            yield return array;
        }
    }
}

Then change your view:

<td>
    @foreach (var partition in Model.ToSizedPartition(3))
    {
         <div>
             @foreach(var item in partition)
             {
                 <img src="@item.Pic" width="80" height="90" />
             }
         </div>
    }
</td>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. As a MVC rookie, it's hard to implement to me.
@ispanak It's not that hard. Just add PartitionExtensions class to your project.
How can i bind ToSizedPartition with my Model? I didn't get how to implement it. I just have a simple model which has 3-4 properties and one of them is Foto property.
@ispanak It's a generic extension method. You don't have to implement it for your type.
1

You can simply use like mentioned below:

<table>
    <tr>
        <td>
            @for (int k = 0; k < Model.Count; k++)
            {
                if (k % 3 == 0 && k != 0)
                {
                    <div style="clear: both"></div>
                }
                <div style="float: left">
                    <img src="@Model[k].Pic" width="80" height="90" />
                </div>
            }
        </td>
    </tr>
</table>

2 Comments

Thanks for your reply. I didnt get the result what i want.
what result are you getting ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.