I am facing issue in handling max with manual input. My implimentation works like , when input value is already equal to max like 13 equal to 13 and i enter more value it becomes i.e 136. i trying its convert to 13 but its work only for 2 digits lenght like 20 converted into 13 but after it becomes 134 or 137
`
<td>
<input min="0.01" class="quantityInput"
type="number" step="0.01"
value="@item.Quantity" @oninput="e => UpdateQuantity(item,e)"
max="@item.MaxQuantity" />
</td>
@code{
private void UpdateQuantity(EditableSaleItemViewModel item, ChangeEventArgs e)
{
var input = e.Value?.ToString();
if (double.TryParse(input, out double newQuantity))
{
if (newQuantity < 0)
{
newQuantity = 0; // Ensure it's not negative
}
else if (newQuantity > item.MaxQuantity)
{
newQuantity = item.MaxQuantity;
}
item.Quantity = newQuantity;
e.Value = item.Quantity;
item.TotalPrice = Math.Round(item.Quantity * item.UnitPrice, 2);
}
else
{
item.Quantity = 0;
item.TotalPrice = 0;
}
totalAmount = Math.Round(editableSaleItems.Sum(i => i.TotalPrice), 2);
StateHasChanged();
}}
`
2
Answers
I can’t create a Minimal Reproducible Example from your code, so here’s a demo of how you could implement your
input
using binding.I had a similar problem by creating a numerical combobox. the problem is in blazor there are two different way to manage onchange, but they will be mixed easily and cause confiusion. this link helped me to undrestand the problem
https://docs.telerik.com/blazor-ui/knowledge-base/value-changed-validation-model
my suggestion is that change
@oninput="e => UpdateQuantity(item,e)"
toValueChanged="CustomerHasChangedEvent"
and addValueExpression="@((@item.Quantity)=> )"
. when you use @oninput you need @bind-value as well…. your tag should seems something like thisso directly you can work with numbers instead of converting string-number.