public ActionResult HinhAnh_SetDefault(int for_sanpham_id, int hinhanh_id)
 {
     if (!this._nhanvien_permission.Contains("hinhanh_edit"))
     {
         return _fail_permission("hinhanh_edit");
     }
     HinhAnhController ctr = new HinhAnhController();
     ctr.set_default(hinhanh_id);
     return RedirectToAction("Index", "AdminSanPham", new { id=for_sanpham_id });
 }
        public ActionResult Submit()
        {
            //get obj id first
            int obj_id = TextLibrary.ToInt(Request["sanpham_id"]);
            //must use same dbcontext neu khong se bao loi (chi can thiet khi co cap nhat thao tac tren doi tuong sanpham trong session)
            SanPhamController ctr = new SanPhamController(this._get_current_dbcontext());

            SanPham obj = (SanPham)Session["sanpham_new_tmp"];
            Boolean un_save = false;
            if (obj != null)
            {
                //un save mode
                un_save = true;
                //add mode
                if (!this._nhanvien_permission.Contains("sanpham_add"))
                {
                    return _fail_permission("sanpham_add");
                }
            }
            else
            {
                //get obj from csdl
                obj = ctr.get_by_id(obj_id);
                //edit mode
                if (!this._nhanvien_permission.Contains("sanpham_edit"))
                {
                    return _fail_permission("sanpham_edit");
                }
                if (obj == null)
                {
                    return RedirectToAction("Index", "AdminSanPhams");
                }
            }

            //assign value
                obj.active = TextLibrary.ToBoolean(Request["sanpham_active"]);
                obj.gia = TextLibrary.ToInt(Request["sanpham_gia"]);
                obj.masp = TextLibrary.ToString(Request["sanpham_masp"]);
                obj.mota = TextLibrary.ToString(Request.Unvalidated["sanpham_mota"]);
                obj.ten = TextLibrary.ToString(Request["sanpham_ten"]);
                //get external object
                HangSXController ctr_hangsx = new HangSXController(ctr._db);
                obj.hangsx = ctr_hangsx.get_by_id(TextLibrary.ToInt(Request["sanpham_hangsx_id"]));

                NhomSanPhamController ctr_nhomsanpham = new NhomSanPhamController(ctr._db);
                obj.nhomsanpham = ctr_nhomsanpham.get_by_id(TextLibrary.ToInt(Request["sanpham_nhomsanpham_id"]));

            //validate properties
            List<String> validate = ctr.validate(obj);

            //action
            if (validate.Count == 0)
            {
                if (!un_save)
                {
                    //update properties first
                    ctr._db.SaveChanges();
                    this._state.Add("edit_ok");
                    this._add_state_tempdata("edit_ok");
                    //get image
                    if (!TextLibrary.ToString(Request["sanpham_upload_hinhanh"]).Equals(""))
                    {
                        HinhAnhController ctr_hinhanh = new HinhAnhController(ctr._db);
                        List<HinhAnh> hinhanh_list = ctr_hinhanh.upload(Server, Request.Files);
                        obj.ds_hinhanh.AddRange(hinhanh_list);
                        ctr._db.SaveChanges();
                    }

                    //successfull redirect
                    return RedirectToAction("Index", "AdminSanPham", new { id = obj.id });
                }
                else
                {
                    //người dùng bấm nút upload hình ảnh chứ không phải nút lưu
                    if (!TextLibrary.ToString(Request["sanpham_upload_hinhanh"]).Equals(""))
                    {
                        //khoan hãy lưu vào csdl
                        HinhAnhController ctr_hinhanh = new HinhAnhController(ctr._db);
                        List<HinhAnh> hinhanh_list = ctr_hinhanh.upload(Server, Request.Files);
                        obj.ds_hinhanh.AddRange(hinhanh_list);
                        //re assign to session
                        Session["sanpham_new_tmp"] = obj;
                        //successfull redirect
                        return RedirectToAction("Index", "AdminSanPham", new { id = 0 });
                    }
                    else
                    {
                        //call add
                        int maxid = ctr.add(obj);
                        //re assign id
                        obj.id = maxid;
                        this._state.Add("add_ok");
                        this._add_state_tempdata("add_ok");
                        //successfull redirect
                        return RedirectToAction("Index", "AdminSanPham", new { id = obj.id });
                    }

                }
            }
            else
            {
                //người dùng bấm nút upload hình ảnh chứ không phải nút lưu
                if (!TextLibrary.ToString(Request["sanpham_upload_hinhanh"]).Equals(""))
                {
                    //khoan hãy lưu vào csdl
                    HinhAnhController ctr_hinhanh = new HinhAnhController(ctr._db);
                    List<HinhAnh> hinhanh_list = ctr_hinhanh.upload(Server, Request.Files);
                    obj.ds_hinhanh.AddRange(hinhanh_list);
                    //re assign to session
                    Session["sanpham_new_tmp"] = obj;
                    //successfull redirect
                    this._add_state_tempdata(validate);
                    return RedirectToAction("Index", "AdminSanPham", new { id = 0 });
                }
            }

            //fail or redirect
            this._state.AddRange(validate);
            ViewBag.State = this._state;
            ViewBag.SanPham = obj;
            ViewBag.Title += " - Submit";
            return View("Index");
        }
 public ActionResult HinhAnh_Delete(int for_sanpham_id, int hinhanh_id)
 {
     if (!this._nhanvien_permission.Contains("chitietsp_delete"))
     {
         return _fail_permission("chitietsp_delete");
     }
     HinhAnhController ctr = new HinhAnhController();
     ctr.delete(hinhanh_id, Server);
     //final action
     return RedirectToAction("Index", "AdminSanPham", new { id = for_sanpham_id });
 }