public int add(MauSac obj)
 {
     this._db.ds_mausac.Add(obj);
     //commit
     this._db.SaveChanges();
     //return ma moi nhat
     return this._db.ds_mausac.Max(x => x.id);
 }
 public ActionResult Add()
 {
     if (!this._nhanvien_permission.Contains("mausac_add"))
     {
         return _fail_permission("mausac_add");
     }
     MauSacController ctr = new MauSacController();
     MauSac obj = new MauSac();
     ViewBag.MauSac = obj;
     ViewBag.Title += " - Add";
     return View("Index");
 }
 public List<string> validate(MauSac obj)
 {
     List<String> re = new List<string>();
     if (obj.giatri.Equals(""))
     {
         re.Add("giatri_fail");
     }
     return re;
 }
        public ActionResult Submit()
        {
            //get nv id first
            int obj_id = TextLibrary.ToInt(Request["mausac_id"]);
            MauSacController ctr = new MauSacController();
            MauSac obj;
            //check mode
            Boolean edit_mode = true;
            if (obj_id == 0)
            {
                //add mode
                if (!this._nhanvien_permission.Contains("mausac_add"))
                {
                    return _fail_permission("mausac_add");
                }

                obj = new MauSac();
                edit_mode = false;
            }
            else
            {
                //edit mode
                if (!this._nhanvien_permission.Contains("mausac_edit"))
                {
                    return _fail_permission("mausac_edit");
                }

                if (ctr.is_exist(obj_id))
                {
                    //update model
                    //get instance of record of table
                    obj = ctr.get_by_id(obj_id);
                }
                else
                {
                    //nvid khong ton tai
                    return RedirectToAction("Index", "AdminMauSacs");
                }
            }
            //assign value
            obj.giatri = TextLibrary.ToString(Request["mausac_giatri"]);
            obj.mota = TextLibrary.ToString(Request["mausac_mota"]);
            obj.mamau = TextLibrary.ToString(Request["mausac_mamau"]);
            obj.active = TextLibrary.ToBoolean(Request["mausac_active"]);
            //validate properties
            List<String> validate = ctr.validate(obj);

            //action
            if (validate.Count == 0)
            {
                if (edit_mode)
                {
                    //update properties first
                    ctr._db.SaveChanges();
                    this._state.Add("edit_ok");
                }
                else
                {
                    //call add
                    int maxid = ctr.add(obj);
                    //re assign id
                    obj.id = maxid;
                    this._state.Add("add_ok");
                }
                //successfull redirect
                return RedirectToAction("Index", "AdminMauSacs");
            }
            //fail redirect
            this._state.AddRange(validate);
            ViewBag.State = this._state;
            ViewBag.MauSac = obj;
            ViewBag.Title += " - Submit";
            return View("Index");
        }