我正在尝试在文本框中输入带小数点的数字,但我收到错误You must enter a valid value
。
有人可以证明我如何修改下面的代码,以确保只有小数点的真实价格和2位小数可以输入表格。
也可以在字段前加上£(&磅)字符,但显然在帖子中,提交的值中省略了£符号?
test.cs中
[DisplayName("Amount")]
public Decimal Amount { get; set; }
Index.cshtml
@Html.TextBoxFor(m => m.Amount, new { @class = "form-control", type = "number", placeholder = "Amount", required = "required" })
答案 0 :(得分:1)
我最终使用了符合HTML5标准的以下代码。
<强> test.cs中强>
[Required]
[DisplayName("Amount")]
[Range(0.01, 100000.00)]
public Decimal Amount { get; set; }
<强> Index.cshtml 强>
@Html.TextBoxFor(m => m.Amount, new { @class = "form-control", type = "number", step = "0.01", max = "100000.00", placeholder = "Amount", required = "required" })
这似乎已经成功了: - )
答案 1 :(得分:0)
使用RegularExpression装饰属性。 另外我注意到你想在你的视图控件中发出Requiered属性,但最好在你的实体中执行它,所以最终的装饰可能如下所示:
[Requiered,RegularExpression(@"^\d+.\d{0,2}$", ErrorMessage = "Amount must be a number with two decimal place")]
public Decimal Amount { get; set; }
...并在您的视图中使用EditorFor
@Html.EditorFor(model => Model.Peso)
对于货币符号,我将采用@DavidG建议的方式