I have the below action method in my controller. But seems like Model.IsValid() is always returning false even the validation conditions are ok and not showing the success message. Any help would be appreciated.
[ActionName("CreateNewEmployeeForm")]
[HttpPost]
public ActionResult SaveEmployee(EmployeeViewModel employee, string btnSubmit)
{
switch (btnSubmit)
{
case "Save Employee":
if (ModelState.IsValid)
{
ViewBag.Message = "Thanks! We got your information.";
return View();
}
else
{
return View();
}
break;
case "Cancel":
return RedirectToAction("EmployeeForm");
}
return new EmptyResult();
}
Following are the validations I have used on entity:
[Required(ErrorMessage ="Please Enter Your Name!")]
[MaxLength(24)]
[MinLength(8)]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Kindly use letters only for name")]
public string EmployeeName { get; set; }
public string Designation { get; set; }
[Required]
[MaxLength(7)]
[MinLength(4)]
[RegularExpression("[^0-9]*$", ErrorMessage = "Salary must be numeric")]
public decimal Salary { get; set; }
[Required(ErrorMessage = "Please Enter Your Date Of Birth!")]
[DataType(DataType.DateTime)]
public DateTime DateOfBirth { get; set; }
[DataType(DataType.DateTime)]
public DateTime DateCreated { get; set; }
case "Cancel", instead you should directly redirect the page toEmployeeFormusing@Html.ActionLink(this will save server resources)