简体   繁体   English

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

[英]Binding the value of a HTML element to a C# property in Blazor

In the blazor application that I am building, I have the following cshtml code which containts a <input> element for the user to enter their name. 在我正在构建的blazor应用程序中,我有以下cshtml代码,其中包含一个<input>元素供用户输入其名称。 I want to be able to bind the value of the input element to the Name property in the c# functions section of the blazor page. 我希望能够将输入元素的值绑定到blazor页面的c#函数部分中的Name属性。

How would I do that? 我该怎么做?

My code is as follows: 我的代码如下:

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

Use the @bind attribute with an input . input使用@bind属性。

@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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM