限于2位小数在TextBoxFor [英] Limit to 2 decimals in TextBoxFor

查看:139
本文介绍了限于2位小数在TextBoxFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

谢谢,

//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"}) 

更新1

在调试器中,我看不到任何小数,wehn我一步一步做内(O @ HTML.Textbofor)认为,价值没有任何小数,但在显示页面时,有4位小数

Update 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

//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"}) 

更新2

我做了一个小的例子: http://goo.gl/RKtwY

推荐答案

我会使用编辑器模板和我的不可以使用我的NHibernate领域模型中的我的看法。我将定义哪些是专门给定视图的要求(在这个情况下,限制的量,以2位小数)视图模型

I would use editor templates and I would not use 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):

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

和则:

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

这篇关于限于2位小数在TextBoxFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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