简体   繁体   中英

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. 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.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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