5. Tables and Forms

Tables show data in rows and columns. A table contains tr (rows), th (header cells) and td (data cells). Forms collect information from users. A form contains inputs such as text boxes, checkboxes and a submit button. In MVC, a form sends its data to a controller action, where model binding turns the fields into a C# object.

Key points

  • Each label should point to an input using for and id.
  • Common input types: text, email, password, number, checkbox, radio, date.
  • The name attribute is what the server uses to read the value.
  • method="post" sends data to the server without showing it in the address.

Example: A table

HTML and CSS Open in editor
<table border="1" cellpadding="8">
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Ana</td>
    <td>20</td>
  </tr>
  <tr>
    <td>Sipho</td>
    <td>22</td>
  </tr>
</table>
Show explanation

th cells are headings and are bold by default. Add another tr with new td cells to add a row.

Example: A form

HTML and CSS Open in editor
<form>
  <label for="name">Name</label><br>
  <input type="text" id="name" name="name" placeholder="Your name"><br><br>

  <label for="email">Email</label><br>
  <input type="email" id="email" name="email"><br><br>

  <input type="checkbox" id="agree" name="agree">
  <label for="agree">I agree</label><br><br>

  <button type="submit">Send</button>
</form>
Show explanation

You can type in the fields. Submitting does nothing here because the preview is sandboxed, but in a real app it would go to a controller.

Try it yourself

Edit the code below and press Run. The result appears in the preview underneath.

Editor
Reset Preview runs in a safe sandbox. Scripts are disabled.
PREVIEW