将HTML元素的值绑定到Blazor中的C#属性

时间:2018-03-26 19:17:41

标签: c# asp.net-core blazor

在我正在构建的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; } = "";
 }

1 个答案:

答案 0 :(得分:3)

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