I’m a huge believer in only developing against exceptions to the rule, so I’m looking at how to avoid: decorating every number with a DisplayFormat attribute; adding attributes to every tag; etc.
For instance, if I show a table, I want every decimal right justified, comma delimited, to two decimal places by rule, which seems to require changes to both html and model if the rule changes. Options on how to attack this? Html generation in the controller which examines a rules repository? DisplayFor extension method + stylesheet + some way to override the stylesheet?
@foreach ( var item in Model )
{
<tr>
<td>@Html.DisplayFor(mi => item.ItemName)</td>
<td align="right">@Html.DisplayFor(mi => item.NewPrice)</td>
<td>@Html.DisplayFor(mi => item.CurrentPrice, "Decimal")</td>
<td align="right">@Html.DisplayFor(mi => item.MyPrice)</td>
<td align="right">@Html.DisplayFor(mi => item.Markup)</td>
</tr>
}
I think what you want to use here is DisplayTemplates.
You would save something like the following partial-view in your Views\Shared\DisplayTemplates folder (sorry for VB syntax):
This code would then be used to render your DisplayFor.