[ACCEPTED]-separate numbers by comma with asp.net mvc-data-annotations

Accepted answer
Score: 14

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


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

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


Html.EditorFor(model => model.GrossFee)

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

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

Score: 5
[DisplayFormat(DataFormatString = "{0:n}")]
public virtual GrossFee? Fee { get; set; }

hope this can help you


Score: 4
drPastDateDetail[strMS] = decValue.ToString();

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

String Test = String.Format("{0:#,#.##}",decValue);
Score: 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

         <td style="color:green; font-weight:bold" >
            Amount Paid: $@TempData["TotalPaid"]


More Related questions