【问题标题】:How to pass the value of an input to another input in blazor webassembly?如何将输入的值传递给 blazor webassembly 中的另一个输入?
【发布时间】: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


    【解决方案1】:

    使用 Producto 类来完成这项工作。只需使用 bind-event on-input 将一个输入绑定到 Cost 并将一个输入绑定到 SalePrice。

    产品模型

    public class Producto
    {
        decimal _cost;
    
        public decimal Cost
        {
            get => _cost;
            set
            {
                 _cost = value;
                 this.SalePrice = _cost * 1.2;
            }
        }
        
        public decimal SalePrice { get; set; }
    }
    

    您可以在模型类中使逻辑尽可能复杂。您的输入控件只会绑定到结果值。

    【讨论】:

      【解决方案2】:

      在@Neil W 的回答下,我做了这个代码实现来做我想做的事,做一个 DTO 来使用属性并且它们会动态改变它们的值,我只是怀疑它是否是唯一的将输入值传递给另一个的方法

      答案:

       public class PrecioProductoDTO
          {
              private decimal precio_de_venta;
              private decimal costo;
              private decimal margen; //Representa un porcentaje
      
              public decimal Costo {
                  get
                  {
                      return costo;
                  }
                  set
      
                  {
                      costo = value;
                      if (margen == 0)
                          precio_de_venta = Costo;
                      else
                          precio_de_venta = Costo * (1 + (margen / 100));
      
                  }
              }
      
              public decimal Margen_Ganancia
              {
                  get
                  {
                      return margen;
                  }
                  set
      
                  {
                      margen = value;
                      if (margen == 0)
                          precio_de_venta = Costo;
                      else
                          precio_de_venta = Costo * (1 + (margen/100));
                  }
              }
      
              public decimal Precio_de_Venta {
                  get
                  {
                      return precio_de_venta;
                  }
                  set
      
                  {
                      precio_de_venta = value;
                      if (Costo != 0)
                          margen = ((precio_de_venta / Costo)-1)*100;
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2014-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-05
        • 1970-01-01
        • 2015-06-28
        • 1970-01-01
        • 2021-03-01
        相关资源
        最近更新 更多