Blazor:如何将文本框的输入作为参数传递给函数



我试图使用blazor向用户呈现一个文本框,然后用用户的输入调用函数Input。这段代码取自"Lambda表达式"。本文档的部分: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);
}
}

这段代码向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);
}
}

这段代码给出了"不能从Microsoft.AspNetCore.Components.ChangeEventArgs转换为字符串">

<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);
}
}

这给出了"语法错误','预期"

这行得通:

<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);
}

最新更新