asp.net-mvc MVC3 Decimal 在编辑时被截断为 2 个小数位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5428289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:02:00  来源:igfitidea点击:

MVC3 Decimal truncated to 2 decimal places on edit

asp.net-mvcasp.net-mvc-3razor

提问by Craig

I'm running MVC3 with Razor and noticed that decimal values are truncated to 2 decimal places when in edit mode. I've managed to get round it by annotating my property with a display format. This doesn't seem like a very good solution as I'll have to remember to do this for every new view I generate (or update my templates).

我正在使用 Razor 运行 MVC3,并注意到在编辑模式下十进制值被截断为 2 个小数位。我设法通过使用显示格式注释我的属性来绕过它。这似乎不是一个很好的解决方案,因为我必须记住为我生成的每个新视图(或更新我的模板)执行此操作。

I have checked the value returned by our service to the controller and it is correct at 1.144, but when bound to the view it comes out as 1.14 in the TextBox

我已经检查了我们的服务返回给控制器的值,它在 1.144 处是正确的,但是当绑定到视图时,它在 TextBox 中显示为 1.14

ViewModel Property

视图模型属性

[Required]
[Display(Name = "Unit Price")]
public decimal UnitPrice { get; set; }

.cshtml Code

.cshtml 代码

@Html.LabelFor(model => model.UnitPrice) 
@Html.EditorFor(model => model.UnitPrice) 
@Html.ValidationMessageFor(model => model.UnitPrice)

If I decorate the property with the following then it works.

如果我用以下内容装饰该属性,则它可以工作。

[DisplayFormat(
               ApplyFormatInEditMode = true, 
               DataFormatString = "{0:0.00###########################}", 
               NullDisplayText = "")]

Any Ideas?

有任何想法吗?

回答by Darin Dimitrov

That's how the default Decimal editor template is defined:

这就是默认的十进制编辑器模板的定义方式

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    private object ModelValue {
        get {
            if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model) {
                return String.Format(
                    System.Globalization.CultureInfo.CurrentCulture,
                    "{0:0.00}", ViewData.ModelMetadata.Model
                );
            }
            return ViewData.TemplateInfo.FormattedModelValue;
        }
    }
</script>
<%= Html.TextBox("", ModelValue, new { @class = "text-box single-line" }) %>

Notice the {0:0.00}format.

注意{0:0.00}格式。

So you have two possibilities:

所以你有两种可能:

  1. Use doubleinstead of decimalas type in your model
  2. Modify the default editor template by creating a custom ~/Views/Shared/EditorTemplates/Decimal.cshtmlwhich might simply look like this:

    @Html.TextBox(
        "", 
        ViewData.TemplateInfo.FormattedModelValue, 
        new { @class = "text-box single-line" }
    )
    
  1. 在模型中使用double而不是decimal作为类型
  2. 通过创建一个自定义~/Views/Shared/EditorTemplates/Decimal.cshtml来修改默认编辑器模板,它可能看起来像这样:

    @Html.TextBox(
        "", 
        ViewData.TemplateInfo.FormattedModelValue, 
        new { @class = "text-box single-line" }
    )
    

You probably might want to modify the display template as well.

您可能还想修改显示模板。

回答by JTech

If you don't need the functionality of the 'EditorFor' HtmlHelper, you can simply swap it out for the 'TextBoxFor' and it shouldn't truncate your decimal value...

如果您不需要 'EditorFor' HtmlHelper 的功能,您可以简单地将其替换为 'TextBoxFor' 并且它不应该截断您的十进制值......

回答by TechSavvySam

IMO, this article has a better option:

IMO,这篇文章有一个更好的选择:

html-editorfor-with-3-decimal-places

html-editorfor-with-3-decimal-places

I used this code to display up to 4 decimal digits in my EditFor:

我使用此代码在我的 EditFor 中显示最多 4 个十进制数字:

    [Display(Name = "Discount Percentage")]
    [Range(0, 100.0)]
    [DisplayFormat(DataFormatString="{0:0.0000}", ApplyFormatInEditMode=true)]
    public Decimal? DiscountPercent { get; set; }