Search code examples
blazorwebassemblyblazor-client-side

How to run additional actions after binding completes in Blazor?


I am trying to figure out how to run an action after binding completes

For example

<input type="text" @bind="@Model.Value" />

<h2>@Value2</h2>

@code
{

void SetNewValue()
{
    Value2 = $"New value {this.Model.Value} added on {DateTime.Now}";
}

string Value2 {get;set;}

}

This is just a simple example. I cannot use onchange since it cannot be used with bind. I tried to use onkeyup but that is executed before binding completes. Blazor doesnt have an option to handle focus lost, so I really dont know how to get this done.


Solution

  •  @page "/"
    
    <input type="text" value="@model.Value" @onchange="@((args) => { model.Value = 
     args.Value.ToString(); SetNewValue(); })" />
    
    <h2>@Value2</h2>
    
    @code
    {
    
    private Model model = new Model();
    
    void SetNewValue()
    {
        Value2 = $"New value {this.model.Value} added on {DateTime.Now}";
    }
    
    string Value2 { get; set; }
    
    
    
    public class Model
    {
        public string Value { get; set; }
    }
    
    }