ASP.NET Core Forms

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:05:00

ASP.NET Core Forms

ASP.NET Core Forms provide a flexible and robust way to handle user input and data submission in web applications. These forms enable developers to capture user information and process it efficiently.

Creating Weakly Typed Forms

Weakly typed forms in ASP.NET Core allow you to create forms without specifying a model class. This approach is useful for simple data collection scenarios.

Example

<form method="post" action="/SubmitForm">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" />
  <input type="submit" value="Submit" />
</form>

Creating Strongly Typed Forms

Strongly typed forms in ASP.NET Core involve binding form fields to a specific model class, making data validation and submission more robust and maintainable.

Example

@model MyWebApp.Models.UserModel
<form asp-action="SubmitForm" method="post">
  <label asp-for="Name">Name:</label>
  <input asp-for="Name" />
  <input type="submit" value="Submit" />
</form>

Field Configuration

Field configuration in ASP.NET Core Forms allows you to specify various attributes, such as labels, placeholders, and validation rules, to customize form elements.

Example

<label for="email">Email:</label>
<input type="email" id="email" name="email" required placeholder="Enter your email" />

Orientation and Layout

You can control the orientation and layout of form elements to create user-friendly and visually appealing forms, adjusting the arrangement of fields and labels.

Example

<div class="form-group">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" />
</div>

Built-in TextBox and TextArea

ASP.NET Core offers built-in HTML helpers for creating textboxes and text areas within forms, simplifying the process of gathering user input.

Example

@Html.TextBox("age", null, new { @class = "form-control" })
@Html.TextArea("comments", 4, 40, new { @class = "form-control" })

Templates

Templates in ASP.NET Core Forms allow you to define reusable custom layouts for form elements, promoting consistency and maintainability.

Example

@model MyWebApp.Pages.Shared.CustomFieldTemplate
<label asp-for="Value"></label>
<input asp-for="Value" />

Hidden Fields

Hidden fields are used to store data on the client side without displaying it. They are valuable for transmitting data like IDs, tokens, or other hidden values.

Example

<input type="hidden" name="csrf_token" value="your_csrf_token_here" />

Validation

ASP.NET Core Forms include built-in validation mechanisms to ensure the data entered meets specified criteria, helping maintain data integrity.

Example

[Required]
[RegularExpression(@"\w+")]
public string Username { get; set; }
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this