During my MVC learning process, my first app started with a basic display form – the data received from the database was in decimal format. So my first stumbling block was figuring out how to make it display as a decimal on screen. The second stumbling block was making it display as a decimal in Edit mode.

Stumbling block #1:
In the Model, I had the following:


 public double MyProperty { get; set; }

All I had to do for the Display mode was to modify it like this:


[DisplayFormat(DataFormatString = "{0:F2}")]
 public double MyProperty { get; set; }

Stumbling block #2:
All I had to do to make it show correctly in Edit mode was modify it the following way:


[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
 public double MyProperty { get; set; }

And that’s all there is to it!