1

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
daveywc
  • 3,136
  • 7
  • 45
  • 58

3 Answers3

1

I would advise you to make a SelectedPersonViewModel, where AnnualSalary is a string and put RegexAttribute above it with a correct regular expression for validation.

You can also internally parse the provided string and save it into a decimal variable and then back to your model (in an action after post)

EDIT: could be something like described in an answer to this question: Decimal values with thousand separator in Asp.Net MVC

Community
  • 1
  • 1
Michal B.
  • 5,676
  • 6
  • 42
  • 70
  • Why change the AnnualSalary in a string and parse it in the action an not leave as Decimal...validation would still work and there will be no need for parsing decimal to string.... ? – George Taskos Mar 19 '12 at 12:13
  • It will allow you to send it to the server in whatever form you accept (regex) and will also get rid of the existing problem. – Michal B. Mar 19 '12 at 12:33
  • Thanks for providing the link in your edit. That led me to find a generic solution from Phil Haack as detailed in my answer below. I cannot really mark your answer as correct, but I have upvoted it. – daveywc Mar 19 '12 at 21:38
0

the model expect a decimal value to be valid, same thing for EF. You can add the decimal value in an hidden field and then display a textbox with the annual salary value formatted with the comma. Then with jquery you can bind the value of the hidden field when the textbox get updated. This way you will pass the decimal value your model is expecting.

Giorgio Minardi
  • 2,765
  • 1
  • 15
  • 11
0

Thanks to the link provided by Michal B. above I eventually found a link to this solution provided by Phil Haack.

I tried numerous other solutions mentioned in the links that I followed, but this is by far the most comprehensive and generic solution that I found.

daveywc
  • 3,136
  • 7
  • 45
  • 58