asp.net-mvc 如何强制 Razor 使 Editorfor 为浮点变量输入数字类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33755560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:30:03  来源:igfitidea点击:

How to force Razor to make Editorfor to input number type for float variable?

asp.net-mvcrazorhtml-inputeditorformodel

提问by Slicksim

Here is my code in MVC 5:

这是我在 MVC 5 中的代码:

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

And here is the html code:

这是 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="">

Not to

<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!

我该怎么办?
感谢您的回复!

回答by Slicksim

Have you tried wrapping your anonymous object in the htmlAttributesof 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.

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

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

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

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

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

回答by tomRedox

You can actually change the default behaviour for the EditorFor for a floatso that it produces type="number"instead of type="text".

您实际上可以为 a 更改 EditorFor 的默认行为,float以便它生成type="number"而不是type="text".

To do that you need to add a custom EditorTemplatefor the Single(notfloat) type to /Views/Shared/EditorTemplates/Single.cshtmlas follows:

为此,您需要EditorTemplateSingle( notfloat) 类型添加自定义,/Views/Shared/EditorTemplates/Single.cshtml如下所示:

@model Single?

@{
    var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
    if (!attributes.ContainsKey("type")) { attributes.Add("type", "number"); }
}
@Html.TextBoxFor(m => m, attributes)

The reason this works is that floatis a C# alias for System.Single(see the Microsoft c# Language Referencefor more details on that). Adding an EditorTemplatecalled Float.cshtml will not work (I tried...).

这样做的原因是它float是 C# 的别名System.Single(有关更多详细信息,请参阅Microsoft c# 语言参考)。添加一个EditorTemplate名为 Float.cshtml 将不起作用(我试过......)。

I got the idea for this from @Stephen Muecke's excellent answer to my question here. He also mentions the idea of creating your own HtmlHelperextension so you could then write @Html.FloatFor(...).

我从@Stephen Muecke在这里对我的问题的出色回答中得到了这个想法。他还提到了创建自己的HtmlHelper扩展的想法,这样你就可以编写@Html.FloatFor(...).

This same approach can also be applied to Decimaland Double, both of which also render type="text"by default.

同样的方法也可以应用于DecimalDouble,这两者也type="text"默认呈现。