Blazor Cheatsheet

Two-Way binding i komponent

@* MyInput.razor *@
@using Microsoft.AspNetCore.Components  

<input type="text"  
	@bind-Value="TextValue"/>  
               
  
@code {  
  
  
    [Parameter]  
    public EventCallback<string> TextValueChanged { get; set; }  
  
    [Parameter]  
    public string? TextValue  
    {  
        get => _textValue;  
        set  
        {  
            if (_textValue == value) return; //Important. Endless loop if not there 
            _textValue = value;  
            TextValueChanged.InvokeAsync(value);  
        }  
    }  
  
    private string? _textValue;  
  
}
<MyInput @bind-TextValue="@MyObject.MyString" />