简体   繁体   English

MVC:将类型为'double'的ViewData对象传递给数字输入字段

[英]MVC: Pass ViewData object with type 'double' to number input field

I'm trying to pass a ViewData object of type double to an input field of type number with 1 decimal place. 我试图将类型为double的ViewData对象传递给数字类型为1的小数位输入字段。 But the input field keeps empty. 但是输入字段保持为空。 What am I doing wrong? 我究竟做错了什么?

<p>
   Debiet factor: <input name="FlowFact" type="number" value="@ViewData["FlowFact"]"  step="0.1" autocomplete="off">
   @String.Format("{0:N1} m³/h", @ViewData["FlowFact"]) <!--Result = 3,1 m³/h-->
</p>

The issue you're facing is what the ViewData["FlowFact"] is returning, which according to your @String.Format("{0:N1} m³/h", @ViewData["FlowFact"]) <!--Result = 3,1 m³/h--> it's 3,1 . 您面临的问题是ViewData["FlowFact"]返回的内容,根据您的@String.Format("{0:N1} m³/h", @ViewData["FlowFact"]) <!--Result = 3,1 m³/h-->3,1

That is not recognised as a number so it won't work. 该号码无法识别,因此无法使用。 Either return a whole number or a decimal number. 返回整数或十进制数。 So instead of a 3,1 , return a 3.1 or just a 3 . 因此,而不是一个的3,1 ,返回3.1或只是一个3 Otherwise change the input type to accept what you are passing. 否则,请更改输入类型以接受您要传递的内容。

I found the solution thanks to jamiedanq. 感谢jamiedanq,我找到了解决方案。

In my controller I was writing a double value to the @ViewData["FlowFact"] object. 在我的控制器中,我正在向@ViewData [“ FlowFact”]对象写入一个双精度值。 But in my view it was returning a value 3,0999 , which is not a correct value for the input type 'Number' because of the ",". 但是在我看来,它返回的值为3,0999,因为“,”,对于输入类型“ Number”而言这不是正确的值。

Instead of passing a double value to the object I've changed it to pass a string value and replace the "," with a ".": 我没有将双精度值传递给对象,而是将其更改为传递字符串值,并将“,”替换为“。”:

Controller: 控制器:

 @ViewData["FlowFact"] = @String.Format("{0:N1}", MyDoubleValue).Replace(",","."); 

View: 视图:

 <p> Debiet factor: <input name="FlowFact" type="number" value="@ViewData["FlowFact"]" step="0.1" autocomplete="off"> </p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为输入类型数字字段设置%符号 - how to set % symbol for input type number field 在输入字段中显示字符串 type=number - Show string in input field type=number 将输入字段(类型=数字)限制为 1 到 20 - limiting input field (type=number) to 1 to 20 有没有办法在 type=“number” 输入字段中捕获字母? - Is there a way to capture Letters in a type=“number” input field? 如何在 type=&#39;number&#39; 的输入字段中允许 1.00 - How to allow 1.00 in input field of type='number' ASP.NET MVC - HTML 错误“没有 IEnumerable 类型的 ViewData<selectlistitem> ”</selectlistitem> - ASP.NET MVC - HTML Error “No ViewData of Type IEnumerable<SelectListItem>” 设置类型为 number 的 Input 字段所需的确切元素数量 - Set the exact number of elements required for an Input field with type number input.type = "数字"; 但仅适用于特定的输入字段 - input.type = "number"; but only for specific input field 为什么 Symfony2 渲染一个<input type="text">代替<input type="number">对于类型为 number 的表单域 - Why is Symfony2 rendering a <input type="text"> instead of <input type="number"> for a form field of type number 限制用户在 type=number 的输入字段中仅键入数字 - Restrict Users to type only numers in an input field with type=number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM