[英]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.