【发布时间】:2021-08-05 20:35:15
【问题描述】:
你好社区我正在处理一个带有十进制数量的表格,我想在输入中写入一个数量并将相同的数量传递给另一个输入,问题是bind-Value 中的第一个输入有cost 属性和第二个sale_price,我也希望在输入输入中写入数量时改变值,所以我做了一个自定义输入,但我无法使绑定正常工作以将成本值传递给sale_price 值
这是我的表格:
<EditForm Model="Producto" OnValidSubmit="OnDataAnnonationsValidated">
<div class="col-sm-4">
<div class="form-group">
<InputLabel for="costo">Costo</InputLabel>
<CampoNumeroOnInput id="costo" Placeholder="Costo" @bind-Value="Producto.Costo" />
<p>Value: @Producto.Costo</p>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<InputLabel for="margen">Margen de Ganancia</InputLabel>
<InputNumber @bind-Value="@Producto.Margen_de_Ganancia" />
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<InputLabel for="precioventa">Precio de Venta</InputLabel>
<InputNumber @bind-Value="@Producto.Precio_de_Venta" />
</div>
</div>
<EditForm />
@code {
[Parameter] public Producto Producto { get; set; }
[Parameter] public EventCallback OnValidSubmit { get; set; }
}
输入数字自定义:
[![@typeparam T
@inherits InputNumber<T>
<input @attributes="AdditionalAttributes"
type="number"
class="form-control form-control-sm"
value="@stringValue"
placeholder="@Placeholder"
@oninput="OnInput"
@onblur="OnBlur" />
@code {
\[Parameter\] public string Placeholder { get; set; }
private string stringValue;
private T lastParsedValue;
protected override void OnParametersSet()
{
// Only overwrite the "stringValue" when the Value is different
if (!Equals(CurrentValue, lastParsedValue))
{
lastParsedValue = CurrentValue;
stringValue = CurrentValueAsString;
}
}
private void OnInput(ChangeEventArgs e)
{
// Update the value
CurrentValueAsString = stringValue = (string)e.Value;
lastParsedValue = CurrentValue;
}
private void OnBlur(FocusEventArgs e)
{
// Overwrite the stringValue property with the parsed value.
// This call Value.ToString(), so the value in the input is well formatted.
// note: Ensure the string value is valid before updating the content
if (!EditContext.GetValidationMessages(FieldIdentifier).Any())
{
stringValue = CurrentValueAsString;
}
}
}
我想做的例子:
【问题讨论】:
标签: c# asp.net-core blazor blazor-webassembly