A web form groups controls so a user can enter data and submit it. In ASP.NET Core, the form’s method and destination determine which controller action or Razor Page handler receives the request. A common pattern uses GET to display the form and POST to process it. section 23.4.3 section 27.2.1
Choose the destination to match the application structure:
- In a Razor view, asp-action generates the form’s action URL using routing. With method="post", submission targets the corresponding POST action; a controller can restrict that action with [HttpPost]. section 3.2.5 section 27.2.1
- In a Razor Page, a POST form with no action submits to the page’s own URL and can be handled by OnPost or OnPostAsync. Use asp-page-handler to select a named handler, such as OnPostRemove. section 23.4.3 section 9.2.1
Controls need names that the receiver can use. In a Razor view, asp-for associates a label and input with a view-model property and generates attributes including the input’s id and name. On submission, an action can inspect Request.Form directly; a Razor Page handler can instead receive a form value through a parameter. An anti-forgery token is also significant: POST requests without the required hidden field are rejected in the protected Razor Page example. section 3.2.5 section 23.4.3 section 27.2.1
Listing 27.6 The contents of the Form.cshtml file in the Views/Form folder
@model Product
@{ Layout = "_SimpleLayout"; }
<h5 class="bg-primary text-white text-center p-2">HTML Form</h5>
<form action="/controllers/form/submitform" method="post">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="Name" value="@Model.Name" />
</div>
<button type="submit" class="btn btn-primary mt-2">Submit</button>
</form>
A complete Razor view form shows the action URL, POST method, named input, model-provided value, and submit button together. section 27.2.1
Submit a product name
Open /controllers/form, change the Name field, and click Submit. The browser sends a POST to the controller’s SubmitForm action. That action reads Request.Form, stores the submitted value in TempData, and redirects to a results view that displays it. This demonstrates the request path; more substantial form-data processing is left for later. section 27.2.1
The same render–submit–display pattern works with Razor Pages. For a cart operation, a form can submit to a named Remove handler rather than a general page handler. Blazor offers another approach: EditForm contains field components and supports validation and form events. section 27.2.2 section 9.2.1 section 36.0











