C# MVC3中带有2位十进制值的文本框显示

C# MVC3中带有2位十进制值的文本框显示,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,文本框十进制值的格式为“00000”(,是十进制分隔符)。我只想要两个小数点。我该怎么做 我的型号有: [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)] public decimal DisplayOrder { get; set; } @Html.EditorFor(model => model.DisplayOrder) 查看: [DisplayFormat(DataFo

文本框十进制值的格式为“00000”(,是十进制分隔符)。我只想要两个小数点。我该怎么做

我的型号有:

 [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
    public decimal DisplayOrder { get; set; }
@Html.EditorFor(model => model.DisplayOrder)
查看:

 [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
    public decimal DisplayOrder { get; set; }
@Html.EditorFor(model => model.DisplayOrder)
但它不起作用


提前感谢

您可以使用正则表达式来完成您的工作

试试下面的方法

[RegularExpression(@"^\d+.\d{0,2}$",ErrorMessage = "Price must can't have more than 2 decimal places")]
public decimal DisplayOrder { get; set; }
有关更多信息,请查看


我希望这将对您有所帮助。

有关MVC3中十进制格式的更多信息,请参阅本主题

这是一个类似问题的链接

如果上述任何一项都不起作用,您可以尝试将数字转换为字符串并将其传递给此函数

 /// <summary>
    /// Formatting a given number to 0,00
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    string NumberFormatting(string number)
    {
        if (number.Contains(','))
        {
            string[] str = number.Split(',');
            if (str[1].Length == 1)
            {
                number += "0";
            }
        }
        else
        {
            number += ",00";
        }
        return DecreaseNumberDigits(number);
    }

    /// <summary>
    /// To allow only two digits after the decimal mark
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    string DecreaseNumberDigits(string number)
    {
        if (number.Contains(','))
        {
            string[] str = number.Split(',');
            if (str[1].Length > 2)
                number = str[0] + "," + str[1].ToCharArray()[0].ToString() + str[1].ToCharArray()[1].ToString();
        }
        return number;
    }
//
///将给定数字格式化为0,00
/// 
/// 
/// 
字符串编号格式设置(字符串编号)
{
if(number.Contains(','))
{
字符串[]str=number.Split(',');
if(str[1]。长度==1)
{
数字+=“0”;
}
}
其他的
{
数字+=“,00”;
}
返回DecreateNumberDigits(数字);
}
/// 
///在小数点后只允许两位数字
/// 
/// 
/// 
字符串递减数字位数(字符串编号)
{
if(number.Contains(','))
{
字符串[]str=number.Split(',');
如果(str[1]。长度>2)
number=str[0]+“,“+str[1].tocharray()[0].ToString()+str[1].tocharray()[1].ToString();
}
返回号码;
}

您能否更具体地说明问题所在。我的文本框的值为0.0000…但我需要0.00,因此无论您在
显示格式中更改了什么,它仍会输出“0.0000”
规格?我将0:00改为此