asp.net mvc - Data annotation is working on editing only -


here controller codes  //         // get: /department/create          public actionresult create()         {             return view(new department());         }           //         // post: /department/create          [httppost]         public actionresult create(department dept)         {             try             {                 dbcontext.addtodepartments(dept);                 dbcontext.savechanges();                 return redirecttoaction("index");             }             catch             {                 return view();             }         }          //         // get: /department/edit/5          public actionresult edit(int id)         {             return view(dbcontext.departments.single(d => d.deptid == id));         }          //         // post: /department/edit/5          [httppost]         public actionresult edit(int id, formcollection collection)         {             var dept = dbcontext.departments.single(d => d.deptid == id);             try             {                 updatemodel(dept);                 dbcontext.savechanges();                 return redirecttoaction("index");             }             catch             {                 return view(dept);             }         }  here partial class       [metadatatype(typeof(departmentmetadata))]     public partial class department     {     }     public class departmentmetadata     {         [required(allowemptystrings = false, errormessage = "department name required.")]         public string deptname { get; set; }     }       required field validation happening on editing only. can insert null 'department name' on eding values, not allowing enter null value. 

you should modelstate.isvalid check when creating record. more on modelstate here


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -