asp.net-mvc TextBoxFor 中限制为 2 位小数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6120375/
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:13:00  来源:igfitidea点击:

Limit to 2 decimals in TextBoxFor

asp.net-mvcasp.net-mvc-3

提问by Kris-I

The code below works fine but, in the textbox the decimal value has this format "0,0000" (, is the decimal separator). I'd like have only 2 decimal. How can I do this ?

下面的代码工作正常,但在文本框中,十进制值的格式为“0,0000”(,是小数点分隔符)。我想要只有两位小数。我怎样才能做到这一点 ?

Thanks,

谢谢,

//Database model used with NHibernate
public class Bank
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName{ get; set; }
    public virtual decimal Amount { get; set; }
}

//MVC Model
public class MyModel
{
    public Bank Bank { get; set; }  
}

//View
@Html.TextBoxFor(m => m.Bank.Amount, new { id = "tbAmount"}) 

Update 1

更新 1

In the debugger, I don't see any decimal, wehn I do step by step inside (o @HTML.Textbofor) the view, the value does not have any decimal but when the page is displayed there are 4 decimals

在调试器中,我没有看到任何小数点,当我在视图中逐步执行(o @HTML.Textbofor)时,该值没有任何小数点,但是当显示页面时有 4 个小数点

//Database model used with NHibernate
public class Bank
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName{ get; set; }
    public virtual decimal Amount { get; set; }
}

//Class for view
public class ViewBank
{
    [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
    public decimal Amount { get; set; }
}

//MVC Model
public class MyModel
{
    public Bank Bank { get; set; }      
    var ViewBank = new ViewBank() { Amount = Bank.Amount};
}

//View
@Html.TextBoxFor(m => m.Amount, new { id = "tbAmount"}) 

回答by Darin Dimitrov

I would use editor templates and I would notuse my NHibernate domain models in my views. I would define view models which are specifically tailored to the requirements of the given view (in this case limiting the amount to 2 decimals):

我会使用编辑器模板,并且不会在我的视图中使用我的 NHibernate 域模型。我将定义专门针对给定视图的要求定制的视图模型(在这种情况下,将数量限制为 2 位小数):

[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public decimal Amount { get; set; }

and then:

进而:

@Html.EditorFor(m => m.Bank.Amount) 

回答by Alexandros B

This works for me

这对我有用

@Html.TextBox("Amount", String.Format("{0:0.00}", Model.Bank.Amount), new { id = "tbAmount"})

EDIT:

编辑:

This is for TextBoxFor (does not work on MVC3)

这是用于 TextBoxFor(不适用于 MVC3)

@{var formated = String.Format("{0:0.00}", Model.Bank.Amount);}
@Html.TextBoxFor(m => m.Bank.Amount, formated, new { id = "tbAmount"})

回答by thelem

In MVC 4 you can now pass the format as the second parameter

在 MVC 4 中,您现在可以将格式作为第二个参数传递

//View
@Html.TextBoxFor(m => m.Bank.Amount, "{0:n2}", new { id = "tbAmount"}) 

回答by davke

If you don't have customized editor template for Decimaltype then EditorFordecorated with DisplayFormatAttributewould probably work out of the box.

如果您没有自定义的Decimal类型编辑器模板,那么EditorFor装饰DisplayFormatAttribute可能会立即使用。

For a custom editor template I ended up using something like:

对于自定义编辑器模板,我最终使用了以下内容:

@model decimal?

@{
    string displayValue;
    if (Model == null)
    {
        displayValue = null;
    }
    else {
        var formatString = (ViewData.ModelMetadata).DisplayFormatString;
        displayValue = formatString == null ? Model.ToString() : string.Format(formatString, Model);
    }

}

<div class="form-group">
    @Html.LabelFor(c => c)
    @Html.TextBoxFor(c => c, new { type = "text", Value = displayValue, @class = "form-control" })
    @Html.ValidationMessageFor(c => c)
</div>

Which works when the property is decorated with DisplayFormatAttributelike so:

当属性被这样装饰时,它会起作用DisplayFormatAttribute

[DisplayFormat(DataFormatString = "{0:n1}", ApplyFormatInEditMode = true), Display(Name = "Commission")]
public decimal? CommissionPercentage { get; set; }