public string Create(string dm, DanhMuc model) { if (ModelState.IsValid) { var danhmuc = db.makeDanhMuc(dm, model); TryValidateModel(danhmuc); if (ModelState.IsValid) { if (model.stt.HasValue) { db.updateSTT(dm, model.stt.Value); } db.Entry(danhmuc).State = EntityState.Added; db.SaveChanges(); // for unit testing model.id = (int)danhmuc.GetType().GetProperty("id").GetValue(danhmuc, null); return("OK"); } } foreach (var state in ModelState.Values) { if (state.Errors.Count > 0) { return(state.Errors[0].ErrorMessage); } } return("ModelState Invalid!"); }