'model validation error message isn't working And Returning Null

I have Model Based on DataBase and here it is

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Freelance.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class User
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public User()
        { 
            this.JobPosts = new HashSet<JobPost>();
            this.Proposals = new HashSet<Proposal>();
            this.Reviews = new HashSet<Review>();
            this.SavedJobs = new HashSet<SavedJob>();
        }

        public int Id { get; set; }

        [RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", ErrorMessage = "Email is not valid.")]
        [Required(ErrorMessage = "Please enter Email"), MaxLength(50)]
        public string Email { get; set; }


        [Display(Name = "User Name")]
        [Required(ErrorMessage = "Please enter User Name"), MaxLength(40)]
        public string UserName { get; set; }

  
        [Required(ErrorMessage = "Please enter Password"), MaxLength(50)]
        public string Password { get; set; }
        [Required(ErrorMessage = "Please enter First Name"), MaxLength(40)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Please enter Last Name"), MaxLength(40)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        [RegularExpression(@"\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$", ErrorMessage = "Phone Number is not valid.")]
        [Required, MaxLength(30)]
        [Display(Name = "Phone Number")]
        public string PhoneNumber { get; set; }
        public string Photo { get; set; }
        [Display(Name = "User Type")]
        public string UserType { get; set; }


        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<JobPost> JobPosts { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Proposal> Proposals { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Review> Reviews { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<SavedJob> SavedJobs { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<UserLogin> UserLogins { get; set; }
    }

    public partial class UserLogin
    {
       

        [RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", ErrorMessage = "Email is not valid.")]
        [Required(ErrorMessage = "Please enter Email"), MaxLength(50)]
        public string Email { get; set; }

        [MinLength(8, ErrorMessage ="Password must be at least 8 length")]
        [Required(ErrorMessage = "Please enter Password"), MaxLength(50)]
        public string Password { get; set; }

     



    }

}

i made the user login so i can login with only email and password because i can't send the User model because of the validation i put inside the class so i made UserLogin class and here is my controller

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login([Bind (Include ="Email,Password")]UserLogin user)
        {
            
                System.Diagnostics.Debug.WriteLine(user.Email);
                System.Diagnostics.Debug.WriteLine(user.Password);
            
            if (ModelState.IsValid)
            {
                System.Diagnostics.Debug.WriteLine("valid");
                var data = db.Users.Where(u => u.Email.ToLower() == user.Email.ToLower() && u.Password == user.Password);
                if (data.Count() == 1)
                {
                    System.Diagnostics.Debug.WriteLine(data.SingleOrDefault().UserType);
                    FormsAuthentication.SetAuthCookie(data.SingleOrDefault().UserName.ToString(), true);
                    return RedirectToAction("CurrentUser");
                }
            }
            ModelState.AddModelError("", "invalid Username or Password");
            return RedirectToAction("Index", "Freelancer");
        }

and here is the form in the view

@model Freelance.Models.ViewModels.HomeViewModel
@using (Html.BeginForm("Login", "User", FormMethod.Post))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        <div class="mb-3">
                            <label for="LoginEmail" class="form-label">Email address</label>
                            @Html.EditorFor(u => u.user.Email, new { htmlAttributes = new { @class = "form-control", @id = "LoginEmail", @type = "Email", @required = "" } })
                            @Html.ValidationMessageFor(u => u.user.Email, "", new { @class = "text-danger" })

                        </div>
                        <div class="mb-3">
                            <label for="LoginPassword" class="form-label">Password</label>

                            @Html.EditorFor(u => u.user.Password, new { htmlAttributes = new { @class = "form-control", @id = "LoginPassword", @type = "Password", @required = "" } })
                            @Html.ValidationMessageFor(u => u.user.Password, "", new { @class = "text-danger" })
                        </div>
                        <div class="mb-3 form-check">
                            <input type="checkbox" onclick="ShowPassword()" class="form-check-input" id="ShowPasswordCheckBox">
                            <label class="form-check-label" for="ShowPasswordCheckBox">Show Password</label>
                        </div>
                        <div class="d-flex justify-content-center">
                            <button type="submit" class="btn btn-primary">Login</button>
                        </div>

                    }

iam using HomeViewModel so i can access different models in the same pages

 public class HomeViewModel
    {
        public List<ViewModel> testModel { get; set; }
        public List<JobPost> posts { get; set; }
        public JobPost post { get; set; }
        public List<User> users { get; set; }

        public User user { get; set; }
        public SavedJob savedPost { get; set; }
        public List<SavedJob> savedPosts { get; set; }

    }

the problem that when i try to submit the form it doesnot show error message even though i put
[MinLength(8)] but in login method it does say that it's not valid model

my question here is why it doesnot show the error message in the html form ?

I did solve it as Serge Said now here is my View :

 @using (Html.BeginForm("Login", "User", FormMethod.Post))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        <div class="mb-3">
                            <label for="LoginEmail" class="form-label">Email address</label>
                            @Html.EditorFor(u => u.userLogin.Email, new { htmlAttributes = new { @class = "form-control", @id = "LoginEmail", @type = "Email", @required = "" } })
                            @Html.ValidationMessageFor(u => u.userLogin.Email, "", new { @class = "text-danger" })

                        </div>
                        <div class="mb-3">
                            <label for="LoginPassword" class="form-label">Password</label>

                            @Html.EditorFor(u => u.userLogin.Password, new { htmlAttributes = new { @class = "form-control", @id = "LoginPassword", @type = "Password", @required = "" } })
                            @Html.ValidationMessageFor(u => u.userLogin.Password, "", new { @class = "text-danger" })
                        </div>
                        <div class="mb-3 form-check">
                            <input type="checkbox" onclick="ShowPassword()" class="form-check-input" id="ShowPasswordCheckBox">
                            <label class="form-check-label" for="ShowPasswordCheckBox">Show Password</label>
                        </div>
                        <div class="d-flex justify-content-center">
                            <button type="submit" class="btn btn-primary">Login</button>
                        </div>

                    }

It Returns null Values to The Controller



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source