Login Register
Code2night
  • Home
  • Blog Archive
  • Tutorial
  • Interview Q&A
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. Model Validation Made Easy in ASP.NET Core MVC 6.0

Model Validation Made Easy in ASP.NET Core MVC 6.0

Date- Jul 11,2022

7889

Free Download Pay & Download
Model Validation

Step 1:  1.)  Open Visual Studio 2022 and Click on Create a new project.

 2.) Search for MVC and select ASP.NET Core Web App(Model-View-Controller)

3.) Select Framework as .NET 6, leave other selections as default and click on Create.

Step 2: Now right-click on the Models folder, "Add” class, and name it Student.

 using System.ComponentModel.DataAnnotations;

namespace CoreValidation.Models
{
    public class Student
    {
        [Key]
        public int Id { get; set; }

        [Required(ErrorMessage = "Please enter name")]
        [StringLength(50)]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter date of birth")]
        [Display(Name = "Date of Birth")]
        [DataType(DataType.Date)]
        public DateTime DateofBirth { get; set; }

        [Required(ErrorMessage = "Choose batch time")]
        [Display(Name = "Batch Time")]
        [DataType(DataType.Time)]
        public DateTime BatchTime { get; set; }

        [Required(ErrorMessage = "Please enter phone number")]
        [Display(Name = "Phone Number")]
        [Phone]
        public string PhoneNumber { get; set; }

        [Required(ErrorMessage = "Please enter email address")]
        [Display(Name = "Email Address")]
        [EmailAddress]
        public string Email { get; set; }

        [Required(ErrorMessage = "Please enter website url")]
        [Display(Name = "Website Url")]
        [Url]
        public string WebSite { get; set; }

        [Required(ErrorMessage = "Please enter password")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [Required(ErrorMessage = "Please enter confirm password")]
        [Display(Name = "Confirm Password")]
        [Compare("Password", ErrorMessage = "Password and confirm password does not match")]
        public string ConfirmPassword { get; set; }
    }
}

Step 3: Now open HomeController under the Controllers folders which are added when we create a new project. Write the following code for Index and New IActionResult methods.

using CoreValidation.Models;
using Microsoft.AspNetCore.Mvc;

namespace CoreValidation.Controllers
{
    public class HomeController: Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Index(Student student)
        {
            if (ModelState.IsValid)
            {

            }
            return View();
        }
    }
}

Step 4: Right-click on the Index IActionResult method. “Add” view with default name “Index” if you don’t have it. Write the following code.

 @model CoreValidation.Models.Student

@{
    ViewData["Title"] = "Home Page";
}

<div class="card">
    <div class="card-header bg-primary text-white text-uppercase">
        <h4>Student Information</h4>
    </div>
    <div class="card-body">
        <form asp-action="Index">
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label asp-for="Name" class="lable-control"></label>
                        <input asp-for="Name" class="form-control" />
                        <span asp-validation-for="Name" class="text-danger"></span>
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="form-group">
                        <label asp-for="DateofBirth" class="lable-control"></label>
                        <input asp-for="DateofBirth" class="form-control" />
                        <span asp-validation-for="DateofBirth" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label asp-for="BatchTime" class="lable-control"></label>
                        <input asp-for="BatchTime" class="form-control" />
                        <span asp-validation-for="BatchTime" class="text-danger"></span>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label asp-for="PhoneNumber" class="lable-control"></label>
                        <input asp-for="PhoneNumber" class="form-control" />
                        <span asp-validation-for="PhoneNumber" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label asp-for="Email" class="lable-control"></label>
                        <input asp-for="Email" class="form-control" />
                        <span asp-validation-for="Email" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label asp-for="WebSite" class="lable-control"></label>
                        <input asp-for="WebSite" class="form-control" />
                        <span asp-validation-for="WebSite" class="text-danger"></span>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label asp-for="Password" class="lable-control"></label>
                        <input asp-for="Password" class="form-control" />
                        <span asp-validation-for="Password" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label asp-for="ConfirmPassword" class="lable-control"></label>
                        <input asp-for="ConfirmPassword" class="form-control" />
                        <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-primary rounded-0">Submit</button>
            </div>
        </form>
    </div>
</div>  

Step 5 : Build and run your application by pressing ctrl+F5


S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to Encrypt and Decrypt Passwords in ASP.NET: A Guide
May 15, 2022
Exception Handling in ASP.NET Core: Best Practices
Aug 05, 2020
HTTP Error 500.31: Failed to Load ASP.NET Core Runtime
Aug 23, 2022
How to Implement PayPal in ASP.NET Core: Ultimate Guide
Oct 30, 2022
Previous in ASP.NET Core
How to Export View as PDF in ASP.NET Core: Complete Guide
Next in ASP.NET Core
Task Scheduling Made Easy in ASP.NET Core: Step-by-Step

Comments

Contents

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • Task Scheduling Made Easy in ASP.NET Core: Step-by-Step 17500 views
  • Implement Stripe Payment Gateway in ASP.NET Core: Step-by-St… 16746 views
  • How to Send Emails with HTML Templates and PDFs in ASP.NET C… 16518 views
  • How to Implement PayPal Payment in ASP.NET Core 8.0 12886 views
  • How to Import Data from Excel in ASP.NET Core: Step-by-Step 12748 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy