In Blazor, using inputnumber the change eventargs value is an empty string when the user types a decimal point

huangapple go评论50阅读模式
英文:

In Blazor, using inputnumber the change eventargs value is an empty string when the user types a decimal point

问题

我目前正在开发一个Blazor应用程序,并遇到了一个问题:

我试图在用户在InputNumber字段中输入后,立即更新我的模型上的一个可空decimal属性,以便我可以立即对输入的数字进行计算。为了做到这一点,我使用oninput来运行一个函数(之前是使用三元运算符内联使用)来收集输入,将其解析为decimal并将其赋值给属性。

这一切都很顺利,直到添加小数点时,ChangeEventArgs.Value突然变为空字符串,而不是预期的值。有人可以告诉我这是为什么吗?以下是我的代码:

<EditForm EditContext="purchaseOrderEditContext">

    <label for="name">发票总额</label>
    <InputNumberOnInput @bind-Value="model.MyDecimalValue" @oninput='(e)=> SetDecimalOnInput(e, model.MyDecimalValue)' />
                        
</EditForm>

以及我的简单函数:

@code {
     public void SetDecimalOnInput(ChangeEventArgs e, decimal? valToSet)
     {
        if (e.Value != null && e.Value.ToString() != string.Empty)
        {
            valToSet = decimal.Parse(e.Value.ToString());
        } 
        else
        {
            valToSet = null;  
        }
    }
}

编辑:原来这只在Firefox中出现问题,在Chrome中可以正常工作!

英文:

I'm currently developing a blazor app and have run into a problem:

I'm trying to update a nullable decimal property on my model as soon as a user types into an inputnumber field so that I can do calculations on the number entered immediately. To do this I'm using the oninput to run a function (previously had this inline using a ternary operator) to collect the input, parse it to a decimal and assign it to the property.

This works fine right up until a decimal point is added whereupon the ChangeEventArgs.Value is suddenly an empty string instead of the expected value. Can anyone tell me why this is happening? Here's my code:

<EditForm EditContext="purchaseOrderEditContext">

    <label for="name">Invoice Total</label>
    <InputNumberOnInput @bind-Value="model.MyDecimalValue" @oninput='(e)=> SetDecimalOnInput(e, model.MyDecimalValue)' />
                        
</EditForm>

and my simple function

@code {
     public void SetDecimalOnInput(ChangeEventArgs e, decimal? valToSet)
     {
        if (e.Value != null && e.Value.ToString() != string.Empty)
        {
            valToSet = decimal.Parse(e.Value.ToString());
        } 
        else
        {
            valToSet = null;  
        }
    }
}

Edit: Turns out that this is only in firefox and works correctly in chrome!

答案1

得分: 1

这实际上是最新版本的Firefox中的一个错误:

https://connect.mozilla.org/t5/ideas/bring-back-the-ability-to-enter-decimal-points-in-numeric-fields/idi-p/32249

英文:

So it turns out that this is actually a bug in the latest firefox version:

https://connect.mozilla.org/t5/ideas/bring-back-the-ability-to-enter-decimal-points-in-numeric-fields/idi-p/32249

huangapple
  • 本文由 发表于 2023年5月25日 22:12:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定