How Are Values Passed From A Form Element in .NET?
Working with forms and the HTML elements is a common task you can expect to perform as a developer in .NET MVC. In this post, I will show you how you can use forms to successfully pass information from the frontend to the backend for processing. So without further adieu, let’s jump in! What are Tag Helpers In .NET MVC, tag helpers enable values to pass from the HTML form element to the controller. Basically, they help you to create and render HTML elements by the use of server-side code. This makes code more easier to understand and quicker to develop. For a robust explanation of tag helpers and how they work, you can read an article by Microsoft here. How Are Values Passed From a Form Element To pass a value from a form element to the backend, you’ll use a form tag helper. It generates a HTML element with action attribute values for interaction with a MVC controller action. For example, in my blog application you can create a comment using a form in the Create.cshtml file for the Comments view, which you can see below: The form tag helper above generates the following HTML: As you can see, MVC generates the action attribute value from the form tag helper attributes asp-controller and asp-action. When the submit button is pressed, the form is submitted to the Create action of the Comments controller, where values are passed using the Comment model as shown in the example below. [HttpPost] public Create([Bind("PostId,Body")] Comment comment) { // Omitted for brevity } In the code you see that the Create action has a HttpPost attribute which signals the MVC runtime environment that we are going to write data. You can also see a Comment parameter with a Bind attribute is passed to the Create action from the form. The Bind attribute ensures that the PostId and Body properties on the Comment parameter are always passed to the Create action. If they are missing, then an error will occur and the Create action will not execute in creating a comment. Wrapping Up You are now more knowledgeable of how values are passed from HTML forms in .NET. Microsoft developed a unique way to pass values in MVC, resulting in a more efficient and simple process. If you found this article helpful, don’t hesitate to leave a comment down below in the comment section. Also, subscribe to the blog to receive updates for new content uploaded to the blog.

Working with forms and the HTML elements is a common task you can expect to perform as a developer in .NET MVC. In this post, I will show you how you can use forms to successfully pass information from the frontend to the backend for processing. So without further adieu, let’s jump in!
What are Tag Helpers
In .NET MVC, tag helpers enable values to pass from the HTML form element to the controller. Basically, they help you to create and render HTML elements by the use of server-side code. This makes code more easier to understand and quicker to develop. For a robust explanation of tag helpers and how they work, you can read an article by Microsoft here.
How Are Values Passed From a Form Element
To pass a value from a form element to the backend, you’ll use a form tag helper. It generates a HTML element with action attribute values for interaction with a MVC controller action.
For example, in my blog application you can create a comment using a form in the Create.cshtml file for the Comments view, which you can see below:
The form tag helper above generates the following HTML:
As you can see, MVC generates the action attribute value from the form tag helper attributes asp-controller and asp-action.
When the submit button is pressed, the form is submitted to the Create action of the Comments controller, where values are passed using the Comment model as shown in the example below.
[HttpPost]
public Create([Bind("PostId,Body")] Comment comment)
{
// Omitted for brevity
}
In the code you see that the Create action has a HttpPost attribute which signals the MVC runtime environment that we are going to write data. You can also see a Comment parameter with a Bind attribute is passed to the Create action from the form.
The Bind attribute ensures that the PostId and Body properties on the Comment parameter are always passed to the Create action. If they are missing, then an error will occur and the Create action will not execute in creating a comment.
Wrapping Up
You are now more knowledgeable of how values are passed from HTML forms in .NET. Microsoft developed a unique way to pass values in MVC, resulting in a more efficient and simple process.
If you found this article helpful, don’t hesitate to leave a comment down below in the comment section. Also, subscribe to the blog to receive updates for new content uploaded to the blog.