如何强制 Razor 使 Editorfor 为浮点变量输入数字类型? [英] How to force Razor to make Editorfor to input number type for float variable?

查看:34
本文介绍了如何强制 Razor 使 Editorfor 为浮点变量输入数字类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 MVC 5 中的代码:

Here is my code in MVC 5:

@Html.EditorFor(model => model.myfloatvalue, new { @type = "number", @min = "0", @step = "0.01", @value = "0" })

这里是html代码:

<input class="text-box single-line" data-val="true" data-val-number="The field Fix Amount must be a number." data-val-required="The Fix Amount field is required." id="myfloatvalue" name="myfloatvalue" type="text" value="">

不要

<input class="text-box single-line" data-val="true" data-val-number="The field Fix Amount must be a number." data-val-required="The Fix Amount field is required." id="myfloatvalue" name="myfloatvalue" type="number" min="0" step="0.01" value="0">

我该怎么办?
谢谢回复!

What should I do?
Thanks for response!

推荐答案

您是否尝试过将匿名对象包装在另一个匿名对象的 htmlAttributes 中?使用 EditorFor/TextBoxFor 时,我相信 MVC 5 是影响编辑器输出的 HTML 属性的唯一方法.

Have you tried wrapping your anonymous object in the htmlAttributes of another anonymous object? When using EditorFor/TextBoxFor, I believe in MVC 5 that's the only way of affecting the HTML attributes output by the editor.

@Html.EditorFor(model => model.myfloatvalue, new { htmlAttributes = new { @type = "number", @min = "0", @step = "0.01", @value = "0" }})

如果您不使用 MVC-5.1 或更高版本,则需要使用 TextBoxFor().注意这里没有使用 htmlAttributes:

If you not using MVC-5.1 or higher, then you will need to use TextBoxFor(). Note no htmlAttributes used here:

@Html.TextBoxFor(m => m.myfloatvalue, new { type = "number", min = "0", step = "0.01" }) // don't set the value attribute

这篇关于如何强制 Razor 使 Editorfor 为浮点变量输入数字类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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