【发布时间】:2019-01-31 18:38:36
【问题描述】:
我在模型类中有两个属性:
public int? IntTest { get; set; }
public decimal? DecimalTest { get; set; }
然后我用它渲染:
@Html.EditorFor(model => model.IntTest, new { htmlAttributes = new { @class = "form-control"} })
@Html.EditorFor(model => model.DecimalTest, new { htmlAttributes = new { @class = "form-control"} })
我希望它们都呈现为数字类型的 html 输入,但小数点没有,我明白了:
<input class="form-control text-box single-line" data-val="true" data-val-number="The field IntTest must be a number." id="IntTest" name="IntTest" type="number" value="" />
<input class="form-control text-box single-line" data-val="true" data-val-number="The field IntTest must be a number." id="DecimalTest" name="DecimalTest" type="text" value="" />
十进制值呈现为type="text",而int 注册为type="number"。
This question 暗示这不是预期的行为,所以我做错了什么吗?
如果这是预期的行为,是否有任何方法可以更改 EditorFor 以将所有小数呈现为 type="number",而不必在每个小数字段的 htmlAttributes 中添加 type = "number"?
【问题讨论】:
-
你需要使用 EditorFor 吗?你考虑过使用
TextBoxFor吗? -
@Shyju 我很确定我对 TextBoxFor 也有相同的行为 - TextBoxFor 是否应该生成 type = "number" 的输入?
标签: razor asp.net-mvc-5 html-input editorformodel