separate numbers by comma with asp.net mvc

霸气de小男生 提交于 2019-12-05 03:27:55

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.

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

hope this can help you

Bhavna Miglani
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;

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>

You should be able to use a Format within tostring

var foo = 1200.2;
var bob = foo.ToString("#,###.00##");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!