將HTML元素的值綁定到Blazor中的C#屬性

asp.net-core blazor c#

在我正在構建的blazor應用程序中,我有以下cshtml代碼,其中包含<input>元素,供用戶輸入其名稱。我希望能夠將input元素的值綁定到blazor頁面的c#functions部分中的Name屬性。

我該怎麼辦?

我的代碼如下:

  @page "/nameUpload"

  <p>
 //This is the input element that I would like to bind
Enter your name: <input type="text" ><br />
  </p>

  @functions {
  //This is the property that I want to bind the input to
  private string Name { get; set; } = "";
 }

一般承認的答案

@bind屬性與input一起使用。

@page "/nameUpload"

<p>
    @* This is the input element that I would like to bind *@
    Enter your name: <input type="text" @bind(name) />
    <br />
</p>

@functions {
    @* This is the property that I want to bind the input to *@
    string name;
}


許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow