I've come across the following piece of code in a Razor view
<table class="key-left">
<tr>
<td>Company</td>
@foreach (Customer c in Model.SelectedInstallers)
{
<td>@c.Name</td>
}
</tr>
<tr class="even">
<td>Address</td>
@foreach (Customer c in Model.SelectedInstallers)
{
<td>
@c.Street<br />
@c.City<br />
@c.County<br />
@c.Postcode
</td>
}
</tr>
<tr>
<td>Contact Details</td>
@foreach (Customer c in Model.SelectedInstallers)
{
<td>
@c.Telephone<br />
@c.Website
</td>
}
</tr>
<tr class="even">
<td>Remove</td>
@foreach (Customer c in Model.SelectedInstallers)
{
<td><a href="#" class="ins-qt-remove remove" title="Remove this Installer from your list" data-account-no="@c.AccountNumber">Remove installer</a></td>
}
</tr>
</table>
Now would it better to stay with this code or change it to do one loop with multiple stringbuilders:
StringBuilder company = new StringBuilder();
StringBuilder address = new StringBuilder();
StringBuilder contact = new StringBuilder();
StringBuilder remove = new StringBuilder();
foreach (Customer customer in Model.SelectedInstallers)
{
company.AppendFormat("\n <td class=\"company\">{0}</td>", customer.Name);
address.AppendFormat("\n <td class=\"address\">{0}<br />{1}<br />{2}<br />{3}</td>", customer.Street, customer.City, customer.County, customer.Postcode);
contact.AppendFormat("\n <td class=\"contact\">{0}<br />{1}</td>", customer.Telephone, customer.Website);
remove.AppendFormat("\n <td class=\"remove\"><a href=\"{0}\" class=\"remove-link\">Remove<span class=\"hide-element\"> {1} from the list</span></a></td>", removeUrl, customer.Name);
}
<table id="compamies-list" class="[email protected]">
<tr class="first-row">
<th scope="row">Company</th>
@Html.Raw(company.ToString())
</tr>
<tr class="even">
<th scope="row">Address</th>
@Html.Raw(address.ToString())
</tr>
<tr class="odd">
<th scope="row">Contact Details</th>
@Html.Raw(contact.ToString())
</tr>
<tr class="even">
<th scope="row">Remove</th>
@Html.Raw(remove.ToString())
</tr>
</table>
Or does it not matter if there is only going to be a maximum of ten installers