FormUsingTagHelpers in Asp.net Core[Full Validation]
Package
"Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2"
Controllers[Folder]
HomeController.cs
using FormUsingTagHelpers.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace FormUsingTagHelpers.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
//If Http Method is Post then all data is store in Employee Model class
[HttpPost]
public IActionResult Index(Employee e)
{
return View();
}
}
}
Models[Folder]
Employee.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace FormUsingTagHelpers.Models
{
public class Employee
{
//it is use to set your own error message
[Required(ErrorMessage ="Name is Must")]
[StringLength(15, MinimumLength =3)]
public string Name { get; set; }
[Required]
[StringLength(15, MinimumLength = 3, ErrorMessage = "Destination must be within 3 and 15 character")]
public Gender Gender { get; set; }
[Required(ErrorMessage ="Age is Must")]
//? can convert integer to nullable integer
[Range(10,50,ErrorMessage ="Name must be within 10 and 50")]
public int? Age { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[RegularExpression("(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,15})$",ErrorMessage ="Please Enter the Strong Password")]
public string Password { get; set; }
public int Salary { get; set; }
[Required]
public string Married { get; set; }
}
//It is use for creating Gender DropDownList
public enum Gender
{
Male, Female
}
}
Views[Folder]
Home
Index.cshtml
@model FormUsingTagHelpers.Models.Employee
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!--Strongly type TagHelper Bind Model Class with View-->
<h1>Index</h1>
<div class="container">
<div>
<div class="row">
<div class="col-sm-4" mx-auto>
<!--asp for bind model class with view-->
<form asp-action="Index" asp-controller="Home" method="post">
<label asp-for="Name"></label>
<input asp-for="Name" placeholder="Enter Your Name" class="form-control" />
<!--It can display validation Error in span tag-->
<span style="color:red;" asp-validation-for="Name"></span>
<br />
<label asp-for="Gender"></label>
<select asp-for="Gender" class="form-control" asp-items="Html.GetEnumSelectList<Gender>()">
<option value="">Select Gender</option>
</select>
<span style="color:red;" asp-validation-for="Gender"></span>
<br />
<label asp-for="Age"></label>
<input asp-for="Age" placeholder="Enter Your Age" class="form-control" />
<span style="color:red;" asp-validation-for="Age"></span>
<br />
<label asp-for="Email"></label>
<input asp-for="Email" placeholder="Enter Your Designation" class="form-control" />
<span style="color:red;" asp-validation-for="Email"></span>
<br />
<label asp-for="Salary"></label>
<input asp-for="Salary" placeholder="Enter Your Salary" class="form-control" />
<span style="color:red;" asp-validation-for="Salary"></span>
<br />
<label asp-for="Password"></label>
<input asp-for="Password" placeholder="Enter Your Salary" class="form-control" />
<span style="color:red;" asp-validation-for="Password"></span>
<br />
<label>Married:</label>
<br />
<label>Yes</label>
<input type="radio" asp-for="Married" value="Yes">
<label>No</label>
<input type="radio" asp-for="Married" value="No">
<br />
<input type="submit" value="Submit" class="btn btn-primary btn-block" />
</form>
</div>
</div>
</div>
</div>
0 Comments