NoWar
NoWar

Reputation: 37633

How to split items into columns (MVC3)

I cannot figure out how to split items onto N columns. I.E. into 3 columns. How it could be done? (No I just did all things vertically)

Thank you for any clue!!!

foreach (var answer in @question.Answers)
{
   @Html.CheckBox("answer_CheckBox_" + answer.ID.ToString(), false, new { @id = answer.ID });  
   <label style="margin-left: 0.5em;">@answer.Title</label>
   <br />                                                                                                         
}

Upvotes: 0

Views: 1119

Answers (1)

Alex
Alex

Reputation: 35409

Use the modulus operator to separate answers into groups divisible by 3:

int i = 1; 
@foreach (var answer in @question.Answers) {
   @Html.CheckBox("answer_CheckBox_" + answer.ID.ToString(), false, new { @id = answer.ID });  
   <label style="margin-left: 0.5em;">@answer.Title</label>

   i % 3 == 0 ? <br/> : ""
   i++
}

note - excuse my razor syntax if it isn't sound...

Upvotes: 2

Related Questions