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

查看:119
本文介绍了如何强制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天全站免登陆