How to create a bootstrap grid through a loop using Razor?

I am using ASP.NET MVC and bootstrap. I have many objects (> 2) in the collection, and each requires a <div class="col-xs-6"> , but with only two columns per row. How to achieve this cycle of use? There is one way, but I'm looking for something better:

 @model List<Object> @using (Html.BeginForm("ActionName", "ControllerName")) { <div class="row"> @for (int i = 0; i < Model.Count; i++) { if (i % 2 != 0) { <div class="row"> <div class="col-xs-6"> @Html.TextBoxFor(o => o[i].Value) </div> </div> } else { <div class="col-xs-6"> @Html.TextBoxFor(o => o[i].Value) </div> } } </div> } 
+5
source share
1 answer

Close the div line and run a new one inside the loop at every second iteration

 <div class="row"> @for (int i = 0; i < Model.Count; i++) { if (i > 0 && i % 2 == 0) { @:</div><div class="row"> // close and start new row } <div class="col-xs-6"> @Html.TextBoxFor(o => o[i].Value) </div> } </div> 
+6
source

Source: https://habr.com/ru/post/1261048/


All Articles