9

I am working on an MVC2 appication.

I use data annotations to validate data (both client side and server side). I have several fields in my model that only allows decimal values. As soon as a user types a decimal values I want it to be converted to comma seperated more readable format. For instance 1200 should be formatted to 1,200 while 500 should stay as it is.

Here is my model:

public virtual GrossFee? Fee { get; set; }

And here is how it is on the view:

%: Html.TextBoxFor(model => model.GrossFee)%>

Any ideas regarding this will be highly appreciated.

Thanks!

gideon
  • 19,329
  • 11
  • 72
  • 113
user629161
  • 767
  • 2
  • 8
  • 13
  • 1
    For pure client side checkout this answer: http://stackoverflow.com/questions/1990512/add-comma-to-numbers-every-three-digits-using-jquery – Michael Gattuso Mar 28 '11 at 17:55

5 Answers5

14

Instead of the Html.TextBoxFor you can use the Html.EditorFor and have the view respect the data annotations like this:

Model:

(I don't know what GrossFee? is but lets assume its a decimal)

[DisplayFormat(DataFormatString = "{0:0,0}")]
public virtual Decimal? Fee { get; set; }

View:

Html.EditorFor(model => model.GrossFee)

You may also need to tweak the HtmlEncode and ApplyFormatInEditMode to suit your particular application.

Anything that converts the textbox contents into comma grouped numbers as soon as entered (I.e. before the post back) would need to be javascript based.

Michael Gattuso
  • 13,020
  • 2
  • 25
  • 29
  • Thanks a lot for your suggestion. I tried like you mentioned and it displays the value of Gross Fee the way I want. [DisplayFormat(DataFormatString = "{0:0,0}", ApplyFormatInEditMode = true, HtmlEncode = true)] public decimal GrossFee { get; set; } But When the form is poasted back to itself the ModelState.IsValid is false and it complains that the value of Gross fee is not in correct format. BTW I have client side validation set up to true and I have a ValidateMessageFor control for each field. Thanks a ton. – user629161 Mar 28 '11 at 19:32
  • This is probably because the values now have commas in them and your validator is expecting a straight number. If the comma thing is important then you will need to consider custom validators or stripping them out before you send them in the form post. – Michael Gattuso Mar 28 '11 at 19:59
  • 2
    Here is the "official" solution to model binding decimals with commas. http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx – Ryan Mar 31 '11 at 03:19
  • The "official" solution from haacked.com worked for me as well! – Julian Dormon May 19 '14 at 19:17
5
[DisplayFormat(DataFormatString = "{0:n}")]
public virtual GrossFee? Fee { get; set; }

hope this can help you

Lee
  • 51
  • 1
4
drPastDateDetail[strMS] = decValue.ToString();

Instead of the above line, if you wish to display the numeric value with a comma, the following code will help you-

String Test = String.Format("{0:#,#.##}",decValue);
drPastDateDetail[strMS]=Test;
Dan
  • 10,531
  • 2
  • 36
  • 55
0

I put this line of code in my Controller

public ActionResult Index()

{

        TempData["TotalPaid"] = totalAmountPaid.ToString("#,###.00");

}

And i put this in my View

   <table>
       <tr>
         <td style="color:green; font-weight:bold" >
            Amount Paid: $@TempData["TotalPaid"]
       </td>
    </tr>        
</table>
Isaac Zahn
  • 11
  • 5
-1

You should be able to use a Format within tostring

var foo = 1200.2;
var bob = foo.ToString("#,###.00##");
isNaN1247
  • 17,793
  • 12
  • 71
  • 118
  • I want it to do it dynamically on client side like if I enter 1200 in the text box and as soon as the text box loses focus, it should format the string to "#,###.##". – user629161 Mar 28 '11 at 17:30