I'm running MVC3 with Razor and noticed that decimal values are truncated to 2 decimal places when in edit mode. I've managed to get round it by annotating my property with a display format. This doesn't seem like a very good solution as I'll have to remember to do this for every new view I generate (or update my templates).
I have checked the value returned by our service to the controller and it is correct at 1.144, but when bound to the view it comes out as 1.14 in the TextBox
ViewModel Property
[Required]
[Display(Name = "Unit Price")]
public decimal UnitPrice { get; set; }
.cshtml Code
@Html.LabelFor(model => model.UnitPrice)
@Html.EditorFor(model => model.UnitPrice)
@Html.ValidationMessageFor(model => model.UnitPrice)
If I decorate the property with the following then it works.
[DisplayFormat(
ApplyFormatInEditMode = true,
DataFormatString = "{0:0.00###########################}",
NullDisplayText = "")]
Any Ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…