I have the following field in my model:
public Decimal AnnualSalary { get; set; }
Displayed in my view as:
@Html.EditorFor(model => model.SelectedPerson.AnnualSalary)
This gives me a data format with 2 decimal places. I would also like a comma thousands separator. So I have created a file named Decimal.cshtml in my Shared | Editor Templates folder that contains the following:
@model Decimal
@Html.TextBox("", Model.ToString("#,##0.00"))
This gives me the display that I want in the editor for existing data. However when I go to save modified data it fails. If I remove the decimal editor template then the data saves fine.
I also note that without the new editor template I do not get a client side validation error if I enter a comma into this text box, however my data is still not saved.
It is as if the client side validation is quite happy to accept the thousands separator, but something on the server side when EF code first is trying to save the data does not like the thousands separator.
This seems like a fairly common requirement, but I am struggling to find a definitive answer anywhere.