skip to Main Content

I would like to combine @bind-Value and @onchange in an input field in blazor. But when i do that i seems to get this error message The attribute names could not be inferred from bind attribute 'bind-Value'. Bind attributes should be of the form 'bind' or 'bind-value' along with their corresponding optional parameters like 'bind-value:event', 'bind:format' etc.. I have found one solution for a work around like this <input class="form-control" type="number" @bind:event="oninput" @onchange="MethodIWantToTrigger" @bind="@MyProperty" />
But the problem i get with this is that is not possible to write 0, ex i cant write 0.01 in the input field. Is there any other solution to make this combination?

2

Answers


  1. @bind is just Razor syntactic sugar. The Razor compiler builds a set of handlers in the compiled C# file.

    Here are two examples of how you can code what I believe you want to do.

    @page "/"
    
    <PageTitle>Home</PageTitle>
    
    <input class="form-control mb-3" @bind:set="this.UpdateValue" @bind:get="_value" />
    
    <input class="form-control mb-3" @bind="_value" @bind:after="DoSomeStuff" />
    
    <div class="alert alert-primary">
        Value: @_value
    </div>
    
    @code{
        private decimal _value;
    
        private Task UpdateValue(decimal value)
        {
            _value = value;
    
            // Do you stuff
    
            return Task.CompletedTask;
        }
    
        private Task DoSomeStuff()
        {
            var myValue = _value;
            // Do you stuff
    
            return Task.CompletedTask;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search