Note that there are some explanatory texts on larger screens.

plurals
  1. POModelState.IsValid becomes false after adding anotations
    primarykey
    data
    text
    <p>In my application If I add annotations to model properties then ModelState.IsValid becomes false. If I remove all annotations then it becomes true. when I add annotations, at that it annotations does work but it still goes to code even if fields are empty which should not happen. I am using mvc4 razor.</p> <p>My model where I have applied annotations</p> <pre><code>using System.ComponentModel.DataAnnotations; namespace HRMDatabaseLayer.Entities { using System; using System.Collections.Generic; public partial class EmployeeDetail { public EmployeeDetail() { this.EmployeeContactDetails = new HashSet&lt;EmployeeContactDetail&gt;(); this.EmployeeDepartments = new HashSet&lt;EmployeeDepartment&gt;(); this.EmployeeEducations = new HashSet&lt;EmployeeEducation&gt;(); this.EmployeeExperiances = new HashSet&lt;EmployeeExperiance&gt;(); this.EmployeeFamilies = new HashSet&lt;EmployeeFamily&gt;(); this.EmployeeLanguages = new HashSet&lt;EmployeeLanguage&gt;(); this.EmployeeSkills = new HashSet&lt;EmployeeSkill&gt;(); } public int Id { get; set; } [Required(ErrorMessage = "First Name is Required")] public string FirstName { get; set; } public string MiddleName { get; set; } [Required(ErrorMessage = "Last Name is Required")] public string LastName { get; set; } [Required(ErrorMessage = "Employee Id is Required")] public string EmployeeId { get; set; } [Required(ErrorMessage = "Gender is Required")] public string Gender { get; set; } public string MaritalStatus { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Required(ErrorMessage = "Birth Date is Required")] public System.DateTime DateOfBirth { get; set; } public string IsAdmin { get; set; } public byte[] Picture { get; set; } [Required(ErrorMessage = "Password is Required")] public string Password { get; set; } public virtual ICollection&lt;EmployeeContactDetail&gt; EmployeeContactDetails { get; set; } public virtual ICollection&lt;EmployeeDepartment&gt; EmployeeDepartments { get; set; } public virtual ICollection&lt;EmployeeEducation&gt; EmployeeEducations { get; set; } public virtual ICollection&lt;EmployeeExperiance&gt; EmployeeExperiances { get; set; } public virtual ICollection&lt;EmployeeFamily&gt; EmployeeFamilies { get; set; } public virtual ICollection&lt;EmployeeLanguage&gt; EmployeeLanguages { get; set; } public virtual ICollection&lt;EmployeeSkill&gt; EmployeeSkills { get; set; } } } </code></pre> <p>This is my view</p> <pre><code>&lt;div&gt; @using (Html.BeginForm("Login", "Login")) { &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;h3&gt;Login&lt;/h3&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Employee Id : &lt;/td&gt; &lt;td&gt; @Html.EditorFor(model =&gt; @model.Employee.EmployeeId) &lt;/td&gt; &lt;td&gt; @Html.ValidationMessageFor(model =&gt; @Model.Employee.EmployeeId) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Password : &lt;/td&gt; &lt;td&gt; @Html.EditorFor(model =&gt; @Model.Employee.Password) &lt;/td&gt; &lt;td&gt; @Html.ValidationMessageFor(model =&gt; @Model.Employee.Password) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="submit" value ="Login" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; @Html.ValidationSummary(true) } &lt;br /&gt; &lt;/div&gt; </code></pre> <p>this is controller code where I am using ModelState.IsValid</p> <pre><code>public ActionResult Login(LoginViewModel empDetails) { if (ModelState.IsValid) { if (ValidateUser(empDetails.Employee.EmployeeId, empDetails.Employee.Password)) { Session["id"] = empRepository.GetEmpId(empDetails.Employee.EmployeeId); FormsAuthentication.SetAuthCookie(empDetails.Employee.EmployeeId, false); return RedirectToAction("Index", "User", new { area = "EmployeeDetails" }); } else { ViewBag.LoginError = "The user name or password provided is incorrect."; } } empDetails.Employee.Password = string.Empty; return View("Index", empDetails); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload