c# - ASP.NET MVC model validation always fails -


i have below action method in controller. seems model.isvalid() returning false validation conditions ok , not showing success message. appreciated.

    [actionname("createnewemployeeform")]     [httppost]     public actionresult saveemployee(employeeviewmodel employee, string btnsubmit)     {         switch (btnsubmit)         {             case "save employee":                 if (modelstate.isvalid)                 {                     viewbag.message = "thanks! got information.";                     return view();                 }                 else                 {                     return view();                 }                 break;             case "cancel":                 return redirecttoaction("employeeform");         }         return new emptyresult();     } 

following validations have used on entity:

    [required(errormessage ="please enter name!")]     [maxlength(24)]     [minlength(8)]     [regularexpression(@"^[a-za-z]+$", errormessage = "kindly use letters name")]     public string employeename { get; set; }      public string designation { get; set; }      [required]     [maxlength(7)]     [minlength(4)]     [regularexpression("[^0-9]*$", errormessage = "salary must numeric")]     public decimal salary { get; set; }      [required(errormessage = "please enter date of birth!")]     [datatype(datatype.datetime)]     public datetime dateofbirth { get; set; }      [datatype(datatype.datetime)]     public datetime datecreated { get; set; } 

have tried this? since modelstate.isvalid checking entered right stuff, getting out of way, switch stuff determine button pushed.

[actionname("createnewemployeeform")] [httppost] public actionresult saveemployee(employeeviewmodel employee, string btnsubmit) {     if (modelstate.isvalid)     {         switch (btnsubmit)         {             case "save employee":                 viewbag.message = "thanks! got information.";             ... want send them after submit data                 break;             case "cancel":             ... want send them if cancel (maybe beginning)                 break;         }     }     return view(); } 

that way switch logic won't in way. check employee model putting break on if statement.

you can put if state determine if btnsubmit == cancel , redirect them away @ point without having deal modelstate.isvalid or switch.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -