Why can't I use ToString("#.##") with a @Html.DisplayFor such as:
@Html.DisplayFor(modelItem => modelItem.Balance.ToString("#.##"))
Why can't I use ToString("#.##") with a @Html.DisplayFor such as:
@Html.DisplayFor(modelItem => modelItem.Balance.ToString("#.##"))
when I've encountered this before, I simply added a Getter to the model that the View consumes.
public string FormattedBalance
{
get
{
return this.Balance.ToString("#.##");
}
}
And then just use it in your view:
@Html.DisplayFor(ModelItem => ModelItem.FormattedBalance)
The DisplayFor renders the default ToString method for the supplied model property.
You can achieve what you want by writing your own @helper.
See http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx