public ActionResult Submit()
        {
            KhachHangController ctr = new KhachHangController();
            List<string> validate = new List<string>();
            //get post value
            int id = TextLibrary.ToInt(Request["khachhang_id"]);
            string tendaydu = TextLibrary.ToString(Request["khachhang_tendaydu"]);
            string matkhau = TextLibrary.ToString(Request["khachhang_matkhau"]);
            string matkhau2 = TextLibrary.ToString(Request["khachhang_matkhau2"]);
            string diachi = TextLibrary.ToString(Request["khachhang_diachi"]);
            string sdt = TextLibrary.ToString(Request["khachhang_sdt"]);
            string email = TextLibrary.ToString(Request["khachhang_email"]);
            //load obj first
            KhachHang obj = ctr.get_by_id(id);
            if (obj == null)
            {
                return RedirectToAction("Index", "FrontKhachHang");
            }
            //pass to obj
            obj.diachi = diachi;
            obj.email = email;
            obj.sdt = sdt;
            obj.tendaydu = tendaydu;
            //validate
            validate.AddRange(ctr.validate(obj,matkhau,matkhau2));
            //update
            if (validate.Count == 0)
            {
                //call update for properties first
                ctr._db.SaveChanges();
                //call set password
                ctr.set_password(obj.id, matkhau2);
                obj = ctr.get_by_id(obj.id);
                this._khachhang = obj;
                //Session["khachhang"] = this._khachhang;//Không cần thiết vì sẽ refresh lại theo id lúc gọi action mới
                validate.Add("edit_ok");
            }

            ViewBag.khachhang = this._khachhang;
            //report back
            ViewBag.State = validate;
            return View("Index");
        }
 public ActionResult Submit()
 {
     //get obj id first
     int obj_id = TextLibrary.ToInt(Request["khachhang_id"]);
     KhachHangController ctr = new KhachHangController();
     LoaiKhachHangController ctr_lkh = new LoaiKhachHangController(ctr._db);
     //khoi tao moi doi tuong
     KhachHang obj;
     Boolean edit_mode = true;
     if (obj_id == 0)
     {
         //add mode
         //check permission
         if (!this._nhanvien_permission.Contains("khachhang_add"))
         {
             return this._fail_permission("khachhang_add");
         }
         obj = new KhachHang();
         edit_mode = false;
     }
     else
     {
         //update mode
         //check permission
         if (!this._nhanvien_permission.Contains("khachhang_edit"))
         {
             return _fail_permission("khachhang_edit");
         }
         //kiem tra ton tai
         if (ctr.is_exist(obj_id))
         {
             //get obj
             obj = ctr.get_by_id(obj_id);
         }
         else
         {
             //id khong ton tai
             return RedirectToAction("Index", "AdminKhachHangs");
         }
     }
     //assign data
     List<string> validate = new List<string>();
     string matkhau = TextLibrary.ToString(Request["khachhang_matkhau"]);
     string matkhau2 = TextLibrary.ToString(Request["khachhang_matkhau2"]);
     obj.email = TextLibrary.ToString(Request["khachhang_email"]);
     obj.sdt = TextLibrary.ToString(Request["khachhang_sdt"]);
     obj.diachi = TextLibrary.ToString(Request["khachhang_diachi"]);
     obj.tendangnhap =  TextLibrary.ToString(Request["khachhang_tendangnhap"]);
     obj.tendaydu =  TextLibrary.ToString(Request["khachhang_tendaydu"]);
     obj.bad = TextLibrary.ToBoolean(Request["khachhang_bad"]);
     obj.active = TextLibrary.ToBoolean(Request["khachhang_active"]);
     //validate
     validate.AddRange(ctr.validate(obj,matkhau,matkhau2));
     //action
     if (validate.Count==0)
     {
         if (edit_mode)
         {
             //call update for properties
             ctr._db.SaveChanges();
             //call set password
             ctr.set_password(obj.id,matkhau2);
             validate.Add("edit_ok");
         }
         else
         {
             //add mode mới có chuyện set loại KH
                 obj.loaikhachhang = ctr_lkh.get_by_id(
                 TextLibrary.ToInt(Request["khachhang_loaikhachhang_id"])
                 );
                 obj.diem = obj.loaikhachhang.mucdiem;
             //call update loaikh first
             obj._Update_LoaiKhachHang(ctr._db);
             //set raw password
             obj.matkhau = matkhau2;
             //call add
             int maxid = ctr.add(obj);
             //re assign id
             obj.id = maxid;
             validate.Add("add_ok");
         }
     }
     obj.loaikhachhang = ctr_lkh.get_by_id(
         TextLibrary.ToInt(Request["khachhang_loaikhachhang_id"])
     );
     ViewBag.KhachHang = obj;
     ViewBag.Title += " - Submit";
     ViewBag.State = validate;
     ViewBag.LoaiKhachHang_List = ctr_lkh.timkiem();
     ViewBag.add_mode = obj_id==0?"1":"0";
     return View("Index");
 }