【发布时间】:2017-04-13 12:27:09
【问题描述】:
我正在尝试在 MVC 中使用输入数字,它正确地接受逗号(我在以逗号作为小数分隔符的文化中)并且 MVC 使用我制作的自定义活页夹接受它。该值已正确保存在数据库中,并返回。
问题是,当将十进制值(看起来像 0.231)传递给数字控件时,我猜它试图用逗号将其格式化为我的文化并且不起作用,所以什么都没有出现。
活页夹在返回到服务器时工作,但在返回到控件时是否需要其他东西才能在返回到页面时工作?
我在剃刀视图中的控制:
@Html.EditorFor(model => model.DecimalValueForExample, new { htmlAttributes = new { @class = "form-control", @type = "number", @step = "any", @min = "0.001", autocomplete = "off" } })
我在视图模型中的属性:
[Display(Name = "DecimalValueForExample", ResourceType = typeof(Properties.Resources))]
[DisplayFormat(DataFormatString = "{0:0.###}", ApplyFormatInEditMode = true)]
[Range(0.001, double.MaxValue, ErrorMessageResourceName = "RangeErrorMessage", ErrorMessageResourceType = typeof(Properties.Resources))]
[Required]
public decimal DecimalValueForExample{ get; set; }
【问题讨论】:
-
我认为 html5
<input type="number" ...>最适用于整数,而不是带小数的数字。在 w3schools 上试用:w3schools.com/html/html_form_input_types.asp -
它适用于十进制,我只是在这里保存了一个示例:w3schools.com/code/tryit.asp?filename=FELCCBVINC0P
-
是的,你是对的。我错过了 step="any" 部分。
标签: c# html asp.net-mvc input razor