onChange event not firing Blazor InputSelect

后端 未结 2 993
遥遥无期
遥遥无期 2020-12-17 01:01

I have the following Code for an InputSelect

               

        
相关标签:
2条回答
  • 2020-12-17 01:12

    The request is not that old, so you may consider the following which works with EditForm (basically hook up onto another event) => at oninput eventhandler you pass The ChangeEventArgs var, and so you can get the value to process

    <InputSelect @bind-Value="@labBook.StockID" @oninput="OnLabbookStockChange" class="form-control">
                                @if (lstStocks != null)
                                    {
                                    <option value="">select...</option>
                                    @foreach (var stock in lstStocks)
                                        {
                                        <option value="@stock.StockID">@stock.TestArticle.TAName</option>
                                        }
                                    }
                            </InputSelect>
    
    0 讨论(0)
  • 2020-12-17 01:28

    Note:

    • InputSelect is a component element, not HTML element, which is why you cannot apply to it the @onchange compiler directive. This directive is applied to elements, usually but not necessarily with the value attribute to form the so- called two way data-binding.

    • @bind-Value is a compiler directive directive instructing the compiler to produce code that enable two-way data binding between components. Applying @bind-Value to the InputSelect component requires you (already done in this case by the Blazor team) to define a parameter property named Value and an EventCallback 'delegate', conventionally named ValueChanged. Value and ValueChanged are properties of the InputSelect component.

    There are a couple of ways to do this:

     <InputSelect ValueExpression="@(()=>comment.Country)" 
                                               Value="@comment.Country" 
                     ValueChanged="@((string value) => OnValueChanged(value ))">
            <option value="">Select country...</option>
            <option value="USA">USA</option>
            <option value="Britain">Britain</option>
            <option value="Germany">Germany</option>
            <option value="Israel">Israel</option>
     </InputSelect>
    

    And

     private Task OnValueChanged(string value)
    {
        // Assign the selected value to the Model 
        comment.Country = value;
       return Task.CompletedTask;
    } 
    

    You can also implement INotifyPropertyChanged.PropertyChanged Event in your Model, as dani herrera has suggested. You do that if you want to do away with the Forms components, including the EditForm, and use the normal HTML tags instead, in which case you'll be able to do something like:

    <input type="text" value="@MyValue" @onchange=OnChange"/>
    

    Of course your model class is going to be thick, and it should communicate with the EditContext....

    Hope this helps...

    0 讨论(0)
提交回复
热议问题