mode...">

Use ToString () with @ Html.DisplayFor

Why can't I use ToString ("#. ##") with @ Html.DisplayFor, for example:

@Html.DisplayFor(modelItem => modelItem.Balance.ToString("#.##")) 
+4
source share
2 answers

when I came across this before, I just added Getter to the model that View uses.

 public string FormattedBalance { get { return this.Balance.ToString("#.##"); } } 

And then just use it in your view:

 @Html.DisplayFor(ModelItem => ModelItem.FormattedBalance) 
+4
source

DisplayFor displays the default ToString method for the specified 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

0
source

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


All Articles