Nathan Dehnel
Nathan Dehnel

Reputation: 133

Blazor: how to pass input from a textbox to a function as argument

I am trying to use blazor to present a textbox to the user and then call the function Input with the user's input. This code is taken from the "Lambda Expressions" section of this document: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-6.0

<div class="input-group">
    <input class="form-control" @onchange="() => Input(path)" />
</div>

@code{
string path;
private async Task Input(string path)
{
    Console.log(path);
}
}

This code passes a null value to Input.

<div class="input-group">
    <input class="form-control" @onchange="path => Input(path)" />
</div>

@code{
string path;
private async Task Input(string path)
{
    Console.log(path);
}
}

This code gives "cannot convert from Microsoft.AspNetCore.Components.ChangeEventArgs to string"

<div class="input-group">
    <input class="form-control" @onchange="path.ToString() => Input(path)" />
</div>

@code{
string path;
private async Task Input(string path)
{
    Console.log(path);
}
}

This gives "Syntax error ',' expected".

Upvotes: 0

Views: 1563

Answers (1)

Nathan Dehnel
Nathan Dehnel

Reputation: 133

This works:

<div class="input-group">
    <input class="form-control" @onchange="@Input" />
</div>


    private async Task Input(Microsoft.AspNetCore.Components.ChangeEventArgs patharg)
    {
        path = (string)patharg.Value;
        Console.log(path);
    }

Upvotes: 1

Related Questions