How to pass information from click event on select to next input

Viewed 54

I am fairly new to Blazor, and I am trying to pass information from one input to next based on click event.

For example, I have the following select element:

<select @onchange="(e => DoStuff(e.Value.ToString()))">
    @foreach (var item in Items)
    {
        <option value="@item.Id"> @item.Name </option>
    }
</select>
</div>
<div class="d-none d-sm-block col-2">
    <input type="number" @bind-value="ItemQuantity"/>
</div>

The idea is when a use clicks on on a selected item, I would like to send and capture that information in the next input (i.e., display Item.Price)

Currently, I am stuck on:

private void DoStuff(string e)
{
    isItemSelected = 1; 
    var Selected = e; 
}

I can't seem to display or use e for some reason. Item is on object that contains Name and Price and I would like to display the price on the second input.

1 Answers

you just miss to cast e.Value to an int;

the below samples does what you wanted.

<select @onchange="DoStuff">
        <option value="0">A</option>
        <option value="1">B</option>
    </select>

<div class="d-none d-sm-block col-2">
    <input type="number" @bind-value="ItemQuantity"/>
</div>
<h1>Selected Value is: @ItemQuantity</h1>

@code{
    int ItemQuantity;


    private void DoStuff(ChangeEventArgs obj)
    {
        if (obj.Value is null) return;

        int.TryParse(obj.Value.ToString(), out var v);
        ItemQuantity = v;
    }
}
Related