LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 5618 min read

Working with Forms

Learn how to build HTML forms with Tag Helpers and handle form submissions in ASP.NET Core MVC.

On this page 6 sections

Tag Helpers

Tag Helpers let you write HTML-like syntax that generates the correct markup automatically, tied to your model's properties.

Building a Form

@model TaskDto
<form asp-action="Create" method="post">
<label asp-for="Title"></label>
<input asp-for="Title" />
<span asp-validation-for="Title"></span>
<label asp-for="Priority"></label>
<input asp-for="Priority" />
<button type="submit">Save</button>
</form>

Handling the Submission

[HttpGet]
public IActionResult Create() => View();
[HttpPost]
public IActionResult Create(TaskDto dto)
{
if (!ModelState.IsValid)
{
return View(dto); // redisplay the form with errors
}
// save the task...
return RedirectToAction("Index");
}
Two Actions, One Route

A typical MVC form pattern uses a [HttpGet] action to display the empty form and a separate [HttpPost] action (same name) to handle the submission — this is called the "GET/POST pattern".

Displaying Validation Errors

<span asp-validation-for="Title"></span> automatically displays any validation error for that specific field, driven by the same data annotations from the Model Binding & Validation lesson.

FAQs

It sends the browser an HTTP redirect to another action — commonly used after a successful form POST, to avoid resubmitting the form if the user refreshes the page (the Post/Redirect/Get pattern).

Summary

  • Tag Helpers (asp-for, asp-action) generate form markup tied to your model.
  • A typical form uses separate GET (display) and POST (handle) actions.
  • RedirectToAction after a successful POST avoids duplicate form submissions.
Next Lesson →

Entity Framework Core Introduction