MVC3 - 对3型小数位的双领先的零 [英] MVC3 - 3 decimal places on type double with leading zero

查看:260
本文介绍了MVC3 - 对3型小数位的双领先的零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在公斤的(double类型或使用别的东西?)的字段。
在编辑视图中,我想用户输入数字到千分位即可。
在显示视图我想公斤出现像560.250

I have a field for weight in Kgs (type double or use something else??). In edit view I would like the user to enter numbers to the thousandth place. In display view I would like the Kgs to appear like 560.250

努力学习MVC3 +剃刀。
乐于探索了jQuery,定期使用前pressions,验证,视图模板,视图模型的...

Trying to learn MVC3 + Razor. Willing to explore JQuery, use of regular expressions, validators, view templates, view models...

根据约定MVC的神奇需要时间来适应。困惑,该方法的使用。

The "magic" of MVC based on conventions takes getting used to. Confused as to which approach to use.

感谢您提前为您的帮助。

Thank you in advance for your help.

推荐答案

您可以在您的视图模型中使用的数据说明:

You could use data annotations on your view model:

[DisplayFormat(DataFormatString = "{0:#,##0.000#}", ApplyFormatInEditMode = true)]
public double? Weight { get; set; }

和在你看来

@Html.EditorFor(x => x.Weight)

在输入字段格式正确的值。

will properly format the value in the input field.

另一种可能性是写的double类型的自定义编辑模板(〜/查看/共享/ EditorTemplates / double.cshtml

Another possibility is to write a custom editor template for the double type (~/Views/Shared/EditorTemplates/double.cshtml):

@model double?
@Html.TextBox("", Model.HasValue ? Model.Value.ToString("#,##0.000#") : "")

,然后在您的视图:

and then in your view:

@Html.EditorFor(x => x.Weight)

或者,如果你不希望覆盖所有模板对所有double类型的应用程序,你可以放入一些自定义模板位置这就像〜/查看/共享/ EditorTemplates / MyFormattedDouble.cshtml ,然后在您的视图:

@Html.EditorFor(x => x.Weight, "MyFormattedDouble")

我个人preFER它使用数据的注释来控制双值的格式第一种方法。

Personally I prefer the first approach which uses data annotations to control the format of the double values.

这篇关于MVC3 - 对3型小数位的双领先的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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