简体   繁体   English

Blazor 应用程序 Razor 页面的 InputText 中的绑定类型双精度值

[英]Binding Type double values in InputText of Blazor app Razor page

I am using InputText for almost all the attributes.我对几乎所有属性都使用 InputText。 Some of the attributes are of type Double.一些属性是 Double 类型。 When I bind the double value in InputText its giving me error.当我在 InputText 中绑定双精度值时,它给了我错误。 How can i resolve this?我该如何解决这个问题? I dont want to use InputNumber.我不想使用 InputNumber。

 <div class="form-group col">
 <label title="The quantity ">Quantity</label>
 <i class="fa fa-info-circle" style="color:dimgrey" title="The quantity"> 
 </i>
 <InputText @bind-Value="@trade.qty" class="form-control" />
 <ValidationMessage For="@(() => trade.qty)" />
 </div>

I am getting error at @trade.qty in <InputText @bind-Value="@trade.qty" class="form-control" /> as its a double value.我在<InputText @bind-Value="@trade.qty" class="form-control" />中的 @trade.qty 处收到错误,因为它是双精度值。

Model: Model:

 public Double qty { get; set; }

I tried something like <InputText @bind-Value="@trade.qty.ToString()" class="form-control" /> It didnt't work.我尝试了类似<InputText @bind-Value="@trade.qty.ToString()" class="form-control" />它没有用。

Use采用

<InputNumber @bind-Value=trade.qty min="1" max="999" />

It is bindable to double.它可以绑定到两倍。

https://docs.microsoft.com/fr-fr/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0 https://docs.microsoft.com/fr-fr/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0

I am not sure why you want that functionality, but what about creating support string property and converting the number inside?我不确定你为什么想要这个功能,但是如何创建支持字符串属性并转换里面的数字呢?

class MyModel
{
    public Double qty { get; set; }
    public string qtyString{
      get=>qty.ToString();
      set{
        if(Double.TryParse(value, out double val))
        qty = val;
         }
      }
}
<InputText @bind-Value="@mod.qtyString"/>

Working solution .工作解决方案

I think both @Alanmakanambra and @Guy at Mercator's answers could both be marked as correct because they answer the question.我认为 @Alanmakanambra 和 @Guy at Mercator 的答案都可以标记为正确,因为他们回答了这个问题。 But I thought it would be worth adding an additional comment.但我认为值得添加额外的评论。

HTML inputs are handled differently by different browsers. HTML 输入由不同的浏览器处理。 Especially in phone browsers, a numerical input is likely to popup a built-in control (like a numpad).特别是在手机浏览器中,数字输入很可能会弹出一个内置控件(如小键盘)。 The Blazor controls render to <input type="number"> , and each browser will present its version of a number input. Blazor 控件呈现为<input type="number"> ,并且每个浏览器将呈现其数字输入的版本。

If you make a custom control (eg using a text input to process numerical input), then you will bypass different devices' native approach to handling numerical input.如果您制作自定义控件(例如使用文本输入来处理数字输入),那么您将绕过不同设备的本机方法来处理数字输入。 In the case of hand phones, this make it hard (or almost impossible) for the user to easily enter numerical information.在手机的情况下,这使得用户很难(或几乎不可能)轻松输入数字信息。

Sometimes you WANT to bypass native systems.有时您想绕过本机系统。 Maybe you have a custom calendar, and you don't want whatever date picker Apple has built-in to an iPhone to ruin your website's appearance.也许您有一个自定义日历,并且您不希望 Apple 内置于 iPhone 的任何日期选择器破坏您网站的外观。 But generally speaking, you shouldn't do that.但一般来说,你不应该这样做。

Short version: use <input type="number> or <InputNumber>简短版本:使用<input type="number><InputNumber>

I do this for float:我这样做是为了浮动:

<input type="number" pattern="/^-?\d+\?\d*$/" readonly="@isReadOnly" onKeyPress="if(this.value.length==8) return false;" 
style="background-color:transparent; border:none; padding: 3px; width: 4em" 
value="@cellvalue.ToString(new System.Globalization.CultureInfo("en-US")).Replace(",",".")" 
@onchange="@((ChangeEventArgs __e) => row.SetField<float>(col, float.Parse(__e.Value.ToString().Replace(",",".")) )" size="8" step=0.01 max="9999" id="number" />

So I use input type, but I had the extra issue of my local numbers culture uses comma and I have that replaced using onchange and value, instead of binding the value directly.所以我使用输入类型,但我有一个额外的问题,我的本地数字文化使用逗号,我使用 onchange 和 value 替换了它,而不是直接绑定值。 Works surprisingly well and it even keeps the comma, but I can type it manually in the field as well using period.效果出奇地好,它甚至保留了逗号,但我也可以使用句点在字段中手动输入它。 Do note that the data I have here, is inside a C# DataTable and this is why the onchange looks the way it does, it will be a lot simpler if you just have a regular double to refer to.请注意,我在这里拥有的数据位于 C# DataTable 中,这就是为什么 onchange 看起来如此,如果您只有一个常规的 double 来引用它会简单得多。

Another advantage is that it adds some number stuff to it, so the step is of you press the arrow up or down on the element, and how much is to be added or subtracted from the value.另一个优点是它添加了一些数字内容,因此步骤是您按下元素上的向上或向下箭头,以及要从值中添加或减去多少。

The "InputNumber" isn't the solution when you need to store a double, because you only can write an integer into this component.当您需要存储双精度时,“InputNumber”不是解决方案,因为您只能将 integer 写入此组件。 So, a solution for this problem (as was proposed by @Alamakanambra) is the use of a support string property and converting the number inside.所以,这个问题的解决方案(正如@Alamakanambra 提出的那样)是使用支持字符串属性并转换里面的数字。 Later, this property can be used into the "InputText" component.稍后,此属性可用于“InputText”组件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM