Skip to content

[Blazor] @bind async #22394

@JvanderStad

Description

@JvanderStad

https://docs.microsoft.com/en-us/aspnet/core/blazor/data-binding?view=aspnetcore-3.1

Data-binding takes place with the following syntax:

<input @bind="CurrentValue" />

@code {
    private string CurrentValue { get; set; }
}

this is equivalent of

<input value="@CurrentValue"
    @onchange="@((ChangeEventArgs __e) => CurrentValue = 
        __e.Value.ToString())" />
        
@code {
    private string CurrentValue { get; set; }
}

I can use the setter of the bound property to handle some extra logic

[Parameter]
public EventCallback<string> Callback { get; set; }

private string _value;
private string CurrentValue
{
	get => _value;
	set {
             _value = value;

             // extra logic

             // await Callback.InvokeAsync(value);   
             // above does not work, we are in the 'sync'-world
        }
}

I find myself starting with this pattern and then getting stuck because I need to call something async. I rewrite the thing to handle the events myself, but it feels a bit... not finished. It becomes a real pain if you have made a reusable component and you need to refactor a lot to make it work with the async call.

Can we introduce a convention to allow async calls with two way binding?

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: DuplicateResolved as a duplicate of another issueStatus: Resolvedaffected-mostThis issue impacts most of the customersarea-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-component-modelAny feature that affects the component model for Blazor (Parameters, Rendering, Lifecycle, etc)feature-razor.languageseverity-minorThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions