当用户单击按钮时,我试图将当前输入的文本值传递给服务器。我已经尝试过下面的代码,但是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" />
和
<input type="text" bind-value-onchange="@YourName" maxlength="20" />
,但没有一个起作用。
我刚刚创建了默认的Blazor应用程序项目,并添加了上面的代码。我忽略了Hello()
方法,因为它对这个问题并不重要。但是无论如何,这是the full project source code的git存储库。更改在FetchData.razor
中;运行项目,然后转到“获取数据”。
您需要将bind-value
与oninput
事件一起使用
<input
type="text"
@bind-value="YOUR_VARIABLE"
@bind-value:event="oninput"
maxlength="20"
/>
您的问题是您使用的是@
错误。
[拥有一个需要控制者的属性时,需要将其传递给@
。 bind
是blazor需要控制的变量,因此正确的是@bind
或@bind-value
。