用asp.net mvc用逗号分隔数字 [英] separate numbers by comma with asp.net mvc

查看:118
本文介绍了用asp.net mvc用逗号分隔数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究MVC2应用程序.

I am working on an MVC2 appication.

我使用数据注释来验证数据(客户端和服务器端).我的模型中有几个字段仅允许十进制值.用户输入十进制值后,我希望将其转换为以逗号分隔的更易读的格式.例如,应将1200格式化为1200,而应将500保持原样.

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.

这是我的模特:

public virtual GrossFee? Fee { get; set; }

这是视图上的样子:

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

任何对此的想法将不胜感激.

Any ideas regarding this will be highly appreciated.

谢谢!

推荐答案

您可以使用Html.EditorFor代替Html.TextBoxFor,并使视图遵循如下数据注释:

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

型号:

(我不知道GrossFee?是什么,但让我们假设其为小数)

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

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

查看:

Html.EditorFor(model => model.GrossFee)

您可能还需要调整HtmlEncode和ApplyFormatInEditMode以适合您的特定应用程序.

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

在输入后(即回发之前)将文本框内容转换为逗号分组的数字的任何操作都需要基于javascript.

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.

这篇关于用asp.net mvc用逗号分隔数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆