decimal scale persisting in viewmodel
I have a viewmodel which seems to be persisting the scale set in the database. I have 3 updateable fields in SQL stored 开发者_JS百科as a decimal(18,4) to represent money. I wish to format the values in my viewmodel as 2 decimal places to make it look cleaner.
my viewmodel looks like this:
[DisplayFormat(DataFormatString = "{0:F2}",ApplyFormatInEditMode=true)]
[DisplayName("Value 1")]
public decimal Value1 { get; set; }
my view looks like this:
<%:Html.EditorFor(m => m.Value1)%>
the values are given a default value of 0 so when I come to display the value in a text box, the value still retains the scale set in the database...
so my output looks like:
Value 1: 0.0000
Value 2: 0.0000
Value 3: 0.0000
if you know why this is happening or how I can resolve it in the view?
If you want metadata such as the [DisplayFormat]
attribute to be used you need the EditorFor/DisplayFor helpers in your views:
<%= Html.EditorFor(x => x.Value1) %>
or:
<%= Html.DisplayFor(x => x.Value1) %>
instead of:
<%= Html.TextBoxFor(x => x.Value1) %>
精彩评论