在事件处理程序中获取当前输入文本

问题描述 投票:0回答:1

当用户单击按钮时,我试图将当前输入的文本值传递给服务器。我已经尝试过下面的代码,但是YourNamenull。为什么不更新?

<input type="text" bind="@YourName"  maxlength="20" />

<button @onclick="() => { Message = ForecastService.Hello(YourName);}">Click</button>

@code {
    string YourName;

我也尝试过

<input type="text" bind-value-oninput="@YourName"  maxlength="20" />

<input type="text" bind-value-onchange="@YourName"  maxlength="20" />

,但没有一个起作用。

我刚刚创建了默认的Blazor应用程序项目,并添加了上面的代码。我忽略了Hello()方法,因为它对这个问题并不重要。但是无论如何,这是the full project source code的git存储库。更改在FetchData.razor中;运行项目,然后转到“获取数据”。

c# data-binding blazor
1个回答
0
投票

您需要将bind-valueoninput事件一起使用

<input 
    type="text" 
    @bind-value="YOUR_VARIABLE" 
    @bind-value:event="oninput" 
    maxlength="20" 
/>

您的问题是您使用的是@错误。

[拥有一个需要控制者的属性时,需要将其传递给@bind是blazor需要控制的变量,因此正确的是@bind@bind-value

© www.soinside.com 2019 - 2024. All rights reserved.