liveBook · Manning

Web form

As explained in Pro ASP.NET Core 7, Tenth Edition

An HTML form is an HTML element that groups controls for collecting user input and submitting it to a server.

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

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Works through form markup, destinations, request handlers, and displayed results; defers fuller form-data processing and model binding.
Examples
Uses an RSVP form, a product editor, controller and Razor Page submissions, and a cart Remove button.
What it assumes
Introduces @model and asp-for while working within an existing ASP.NET Core application with views or Razor Pages.
What you can do after
Build it from scratch using a Razor view or Razor Page, then submit input to a POST handler and inspect the result.
How it connects
Links forms to routing, Tag Helpers, HTTP methods, model binding, anti-forgery protection, TempData, and validation.

Also explained in

Compare how 12 books explain web form →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.