Generating a form using HTML Helpers

We can generate the same form using HTML Helpers as follows (HTML.BeginForm, @Html.Label, and @Html.TextBox generate the HTML form, label, and textbox elements, respectively):

@using (Html.BeginForm()){  <table>    <tr>      <td>@Html.Label("Name")</td>      <td>@Html.TextBox("txtName")</td>    </tr>    <tr>      <td>@Html.Label("Age")</td>      <td>@Html.TextBox("txtAge")</td>    </tr>    <tr>      <td colspan="2"><input type="submit" value="Submit">      </td>    </tr>  </table>}
The form tag will automatically close when you use the block.

The following screenshot gives a glimpse of the task of HTML Helpers:

You might wonder why we need to use HTML ...

Get ASP.NET Core 2 Fundamentals now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.