Search code examples
razorblazorblazor-server-side

How to use Blazor bind:set and bind:get correctly?


I am trying to use the new blazor value-binding with separete get and set. Somehow my code does not work and I get this compiler-exception:

Argument 3: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<string>' to 'System.Action<string?>'

My code:

<input type="text"
    @bind:get="text"
    @bind:set="SetValue" />
    
@code {
    private string text = string.Empty;

    private void SetValue(string value)
    {
        text = value;
    }
}

I tried to use this.text in the @bind:get and i tried to use @bind-value but it did not change anything

Meanwhile, this code works totally fine:

<input type="text"
    @bind="Text" />

@code {
    private string Text
    {
        get => text;
        set => SetValue(value);
    }

    private string text;

    private void SetValue(string text)
    {
        this.text = text;
    }
}

Update: The second code snipped does not work anymore (it does not show the value correct in the input field)


Solution

  • You need to make sure you have the latest updates to Visual Studio and Net7.0. @bind:after is another story!

    Pre March 2023 releases had the issue you have highlighted, but it was fixed in the latest 17.5 release. See the screen shots below.

    No Errors showing:

    strong text

    Running:

    enter image description here

    enter image description here