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

时间:2019-10-08 08:40:30

标签: c# data-binding blazor

我试图在用户单击按钮时将当前输入的文本值传递给服务器。我已经尝试过下面的代码,但是class ItemDisplay { Item currentItem; Item[] suggestedItems; Item[] randomlySelectedItems; } YourName。为什么不更新?

null

我也尝试过

<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" />

,但没有一个起作用。

我刚刚创建了默认的Blazor应用程序项目,并添加了上面的代码。我忽略了<input type="text" bind-value-onchange="@YourName" maxlength="20" /> 方法,因为它对这个问题并不重要。但是无论如何,这是the full project source code的git存储库。更改在Hello()中;运行项目,然后转到“获取数据”。

1 个答案:

答案 0 :(得分:0)

您需要在bind-value事件中使用oninput

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

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

当您拥有blazor需要控制的属性时,您需要使用@传递它。 bind是blazor需要控制的变量,因此正确的变量是@bind@bind-value