izengod
izengod

Reputation: 1156

ASP.NET MVC model validation always fails

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; }

Upvotes: 0

Views: 1344

Answers (4)

Kasama Chenkhaow
Kasama Chenkhaow

Reputation: 54

it seems like your regex is not correct, the hat(^) should be outside of the bracket?

[RegularExpression("[^0-9]*$", ErrorMessage = "Salary must be numeric")]

Hope this help. :)

Upvotes: 0

nocturns2
nocturns2

Reputation: 661

Have you tried this? Since your ModelState.IsValid is only checking that they entered the right stuff, getting it out of the way, then do the switch stuff to determine what button they pushed.

[ActionName("CreateNewEmployeeForm")]
[HttpPost]
public ActionResult SaveEmployee(EmployeeViewModel employee, string btnSubmit)
{
    if (ModelState.IsValid)
    {
        switch (btnSubmit)
        {
            case "Save Employee":
                ViewBag.Message = "Thanks! We got your information.";
            ... where you want to send them after they submit their data
                break;
            case "Cancel":
            ... where you want to send them if they cancel (maybe back to the beginning)
                break;
        }
    }
    return View();
}

That way your switch logic won't get in the way. Check the employee model by putting a break on the if statement.

You can also put an if state to determine early if the btnSubmit == Cancel and redirect them away at that point without having to deal with the ModelState.IsValid or the switch.

Upvotes: 0

Yanga
Yanga

Reputation: 3002

You can try which validation cause the error:

foreach (ModelState state in employee.ModelState.Values.Where(x => x.Errors.Count > 0))
{

}

Upvotes: 1

Dabbas
Dabbas

Reputation: 3230

Inspect you ModelState to see what are the error messages, another thing to mention, it is not a clean way to write one action method to serve two buttons doing two different thingsm you should create two actions.

Upvotes: 0

Related Questions