public ActionResult ThemMoi(SanPham objSanPham, List <string> SizeId, List <string> GenderId1, List <string> ColorId, List <string> HandId, List <string> FlexId, List <string> LoftId, List <string> BounceId, List <string> LengthId, List <string> ShaftId, List <string> WidthId, List <string> WaistId, List <string> InseamId, HttpPostedFileBase fileUpload, HttpPostedFileBase[] files)
        {
            if (objSanPham != null)
            {
                //Gán thông tin cho sản phẩm
                objSanPham.NgayTao     = DateTime.Now;
                objSanPham.NgayCapNhat = DateTime.Now;
                NguoiDung objUser = (NguoiDung)Session[ConstantData.USER_SESSION];

                if (objUser != null)
                {
                    objSanPham.NguoiTaoId = objUser.Id;
                }

                //Nếu chọn ảnh đại diện
                if (fileUpload != null && fileUpload.ContentLength > 0)
                {
                    fileUpload.SaveAs(Server.MapPath("~/Content/Images/SanPham/" + fileUpload.FileName));
                    objSanPham.AnhDaiDien = fileUpload.FileName;
                }
                if (HandId != null)
                {
                    string thuanTay = string.Join(";", HandId);
                    objSanPham.RightLeftHand = thuanTay;
                }

                if (GenderId1 != null)
                {
                    string gioiTinh = string.Join(";", GenderId1);
                    objSanPham.GenderName = gioiTinh;
                }

                #region Thêm ảnh sản phẩm

                List <AnhSanPham> lstAnhSP = new List <AnhSanPham>();
                foreach (var item in files)
                {
                    var objAnhSanPham = new AnhSanPham();

                    if (item != null && item.ContentLength > 0)
                    {
                        string fileName = System.IO.Path.GetFileName(item.FileName);

                        item.SaveAs(Server.MapPath("~/Content/Images/SanPham/AnhSP/" + fileName));

                        objAnhSanPham.Avatar  = fileName;
                        objAnhSanPham.SanPham = objSanPham;
                        objAnhSanPham.TenAnh  = fileName;
                        lstAnhSP.Add(objAnhSanPham);
                    }
                }

                if (lstAnhSP != null && lstAnhSP.Count > 0)
                {
                    objSanPham.AnhSanPhams = lstAnhSP;
                }

                //Thêm vào model
                DataProvider.Entities.SanPhams.Add(objSanPham);

                #endregion Thêm ảnh sản phẩm

                #region Thêm các size của sản phẩm

                if (SizeId != null && SizeId.Count > 0)
                {
                    for (int i = 0; i < SizeId.Count; i++)
                    {
                        SanPham_Size objSize = new SanPham_Size();
                        objSize.SanPhamId = objSanPham.Id;
                        objSize.SizeId    = int.Parse(SizeId[i]);
                        DataProvider.Entities.SanPham_Size.Add(objSize);
                    }
                }

                #endregion Thêm các size của sản phẩm

                #region Thêm các màu sắc của sản phẩm

                if (ColorId != null && ColorId.Count > 0)
                {
                    for (int i = 0; i < ColorId.Count; i++)
                    {
                        SanPham_Color obj     = new SanPham_Color();
                        int           colorId = 0;
                        int.TryParse(ColorId[i], out colorId);

                        obj.ColorId = colorId;
                        obj.SanPham = objSanPham;

                        //Thêm màu cho sp
                        DataProvider.Entities.SanPham_Color.Add(obj);
                    }
                }

                #endregion Thêm các màu sắc của sản phẩm

                #region Thêm các Flex của sản phẩm

                if (FlexId != null && FlexId.Count > 0)
                {
                    for (int i = 0; i < FlexId.Count; i++)
                    {
                        SanPham_Flex obj    = new SanPham_Flex();
                        int          flexId = 0;
                        int.TryParse(FlexId[i], out flexId);
                        obj.FlexId  = flexId;
                        obj.SanPham = objSanPham;
                        DataProvider.Entities.SanPham_Flex.Add(obj);
                    }
                }

                #endregion Thêm các Flex của sản phẩm

                #region Thêm các Loft của sản phẩm

                if (LoftId != null && LoftId.Count > 0)
                {
                    for (int i = 0; i < LoftId.Count; i++)
                    {
                        SanPham_Loft obj    = new SanPham_Loft();
                        int          loftId = 0;
                        int.TryParse(LoftId[i], out loftId);
                        obj.LoftId  = loftId;
                        obj.SanPham = objSanPham;
                        DataProvider.Entities.SanPham_Loft.Add(obj);
                    }
                }

                #endregion Thêm các Loft của sản phẩm

                #region Thêm các Bounce của sản phẩm

                if (BounceId != null && BounceId.Count > 0)
                {
                    for (int i = 0; i < BounceId.Count; i++)
                    {
                        SanPham_Bounce obj      = new SanPham_Bounce();
                        int            bounceId = 0;
                        int.TryParse(BounceId[i], out bounceId);
                        obj.BounceId = bounceId;
                        obj.SanPham  = objSanPham;
                        DataProvider.Entities.SanPham_Bounce.Add(obj);
                    }
                }

                #endregion Thêm các Bounce của sản phẩm

                #region Thêm các Length của sản phẩm

                if (LengthId != null && LengthId.Count > 0)
                {
                    for (int i = 0; i < LengthId.Count; i++)
                    {
                        SanPham_LengthSize obj = new SanPham_LengthSize();
                        int lengthId           = 0;
                        int.TryParse(LengthId[i], out lengthId);
                        obj.LengthId = lengthId;
                        obj.SanPham  = objSanPham;
                        DataProvider.Entities.SanPham_LengthSize.Add(obj);
                    }
                }

                #endregion Thêm các Length của sản phẩm

                #region Thêm các Shaft của sản phẩm

                if (ShaftId != null && ShaftId.Count > 0)
                {
                    for (int i = 0; i < ShaftId.Count; i++)
                    {
                        SanPham_Shaft obj     = new SanPham_Shaft();
                        int           shaftId = 0;
                        int.TryParse(ShaftId[i], out shaftId);
                        obj.ShaftId = shaftId;
                        obj.SanPham = objSanPham;
                        DataProvider.Entities.SanPham_Shaft.Add(obj);
                    }
                }

                #endregion Thêm các Shaft của sản phẩm

                #region Thêm các Width của sản phẩm

                if (WidthId != null && WidthId.Count > 0)
                {
                    for (int i = 0; i < WidthId.Count; i++)
                    {
                        SanPham_Width obj     = new SanPham_Width();
                        int           widthId = 0;
                        int.TryParse(WidthId[i], out widthId);
                        obj.WidthId = widthId;
                        obj.SanPham = objSanPham;
                        DataProvider.Entities.SanPham_Width.Add(obj);
                    }
                }

                #endregion Thêm các Width của sản phẩm

                #region Thêm các Waist của sản phẩm

                if (WaistId != null && WaistId.Count > 0)
                {
                    for (int i = 0; i < WaistId.Count; i++)
                    {
                        SanPham_Waist obj     = new SanPham_Waist();
                        int           waistId = 0;
                        int.TryParse(WaistId[i], out waistId);
                        obj.WaistId = waistId;
                        obj.SanPham = objSanPham;
                        DataProvider.Entities.SanPham_Waist.Add(obj);
                    }
                }

                #endregion Thêm các Waist của sản phẩm

                #region Thêm các Inseam của sản phẩm

                if (InseamId != null && InseamId.Count > 0)
                {
                    for (int i = 0; i < InseamId.Count; i++)
                    {
                        SanPham_Inseam obj      = new SanPham_Inseam();
                        int            inseamId = 0;
                        int.TryParse(InseamId[i], out inseamId);
                        obj.InseamId = inseamId;
                        obj.SanPham  = objSanPham;
                        DataProvider.Entities.SanPham_Inseam.Add(obj);
                    }
                }

                #endregion Thêm các Inseam của sản phẩm

                //Lưu lại thông tin
                DataProvider.Entities.SaveChanges();

                return(RedirectToAction("Sua", new { id = objSanPham.Id }));
            }

            LoadDropDownList();

            return(View());
        }
        public ActionResult Sua(SanPham objSanPham, List <string> SizeId, List <string> GenderId1, List <string> ColorId, List <string> HandId, List <string> FlexId, List <string> LoftId, List <string> BounceId, List <string> LengthId, List <string> ShaftId, List <string> WidthId, List <string> WaistId, List <string> InseamId, HttpPostedFileBase fileUpload, HttpPostedFileBase[] files)
        {
            SanPham objSP = DataProvider.Entities.SanPhams.Where(p => p.Id == objSanPham.Id).First();

            if (objSanPham != null)
            {
                //Gán thông tin cho sản phẩm
                objSanPham.NgayCapNhat = DateTime.Now;
                //Nếu chọn ảnh đại diện
                if (fileUpload != null && fileUpload.ContentLength > 0)
                {
                    fileUpload.SaveAs(Server.MapPath("~/Content/Images/SanPham/" + fileUpload.FileName));
                    objSanPham.AnhDaiDien = fileUpload.FileName;
                }
                else
                {
                    objSanPham.AnhDaiDien = objSP.AnhDaiDien;
                }
                if (HandId != null)
                {
                    string thuanTay = string.Join(";", HandId);
                    objSanPham.RightLeftHand = thuanTay;
                }

                if (GenderId1 != null)
                {
                    string gioiTinh = string.Join(";", GenderId1);
                    objSanPham.GenderName = gioiTinh;
                }

                #region Thêm ảnh sản phẩm

                List <AnhSanPham> lstAnhSP = new List <AnhSanPham>();
                foreach (var item in files)
                {
                    var objAnhSanPham = new AnhSanPham();

                    if (item != null && item.ContentLength > 0)
                    {
                        string fileName = System.IO.Path.GetFileName(item.FileName);

                        item.SaveAs(Server.MapPath("~/Content/Images/SanPham/AnhSP/" + fileName));

                        objAnhSanPham.Avatar    = fileName;
                        objAnhSanPham.SanPham   = objSP;
                        objAnhSanPham.SanPhamId = objSP.Id;
                        objAnhSanPham.TenAnh    = fileName;
                        lstAnhSP.Add(objAnhSanPham);
                    }
                }

                if (lstAnhSP != null && lstAnhSP.Count > 0)
                {
                    objSP.AnhSanPhams = lstAnhSP;
                    DataProvider.Entities.AnhSanPhams.AddRange(lstAnhSP);
                }

                #endregion Thêm ảnh sản phẩm

                #region Thêm các size của sản phẩm

                if (SizeId != null && SizeId.Count > 0)
                {
                    //Xóa cũ đi
                    if (objSP.SanPham_Size != null && objSP.SanPham_Size.Count > 0)
                    {
                        DataProvider.Entities.SanPham_Size.RemoveRange(objSP.SanPham_Size);
                    }

                    for (int i = 0; i < SizeId.Count; i++)
                    {
                        SanPham_Size objSize = new SanPham_Size();
                        objSize.SanPhamId = objSP.Id;
                        objSize.SizeId    = int.Parse(SizeId[i]);
                        DataProvider.Entities.SanPham_Size.Add(objSize);
                    }
                }

                #endregion Thêm các size của sản phẩm

                #region Thêm các màu sắc của sản phẩm

                if (ColorId != null && ColorId.Count > 0)
                {
                    if (objSP.SanPham_Color != null && objSP.SanPham_Color.Count > 0)
                    {
                        DataProvider.Entities.SanPham_Color.RemoveRange(objSP.SanPham_Color);
                    }

                    for (int i = 0; i < ColorId.Count; i++)
                    {
                        SanPham_Color obj     = new SanPham_Color();
                        int           colorId = 0;
                        int.TryParse(ColorId[i], out colorId);

                        obj.ColorId = colorId;
                        obj.SanPham = objSP;

                        //Thêm màu cho sp
                        DataProvider.Entities.SanPham_Color.Add(obj);
                    }
                }

                #endregion Thêm các màu sắc của sản phẩm

                #region Thêm các Flex của sản phẩm

                if (FlexId != null && FlexId.Count > 0)
                {
                    if (objSP.SanPham_Flex != null && objSP.SanPham_Flex.Count > 0)
                    {
                        DataProvider.Entities.SanPham_Flex.RemoveRange(objSP.SanPham_Flex);
                    }

                    for (int i = 0; i < FlexId.Count; i++)
                    {
                        SanPham_Flex obj    = new SanPham_Flex();
                        int          flexId = 0;
                        int.TryParse(FlexId[i], out flexId);
                        obj.FlexId  = flexId;
                        obj.SanPham = objSP;
                        DataProvider.Entities.SanPham_Flex.Add(obj);
                    }
                }

                #endregion Thêm các Flex của sản phẩm

                #region Thêm các Loft của sản phẩm

                if (LoftId != null && LoftId.Count > 0)
                {
                    if (objSP.SanPham_Loft != null && objSP.SanPham_Loft.Count > 0)
                    {
                        DataProvider.Entities.SanPham_Loft.RemoveRange(objSP.SanPham_Loft);
                    }

                    for (int i = 0; i < LoftId.Count; i++)
                    {
                        SanPham_Loft obj    = new SanPham_Loft();
                        int          loftId = 0;
                        int.TryParse(LoftId[i], out loftId);
                        obj.LoftId  = loftId;
                        obj.SanPham = objSP;
                        DataProvider.Entities.SanPham_Loft.Add(obj);
                    }
                }

                #endregion Thêm các Loft của sản phẩm

                #region Thêm các Bounce của sản phẩm

                if (BounceId != null && BounceId.Count > 0)
                {
                    if (objSP.SanPham_Bounce != null && objSP.SanPham_Bounce.Count > 0)
                    {
                        DataProvider.Entities.SanPham_Bounce.RemoveRange(objSP.SanPham_Bounce);
                    }

                    for (int i = 0; i < BounceId.Count; i++)
                    {
                        SanPham_Bounce obj      = new SanPham_Bounce();
                        int            bounceId = 0;
                        int.TryParse(BounceId[i], out bounceId);
                        obj.BounceId = bounceId;
                        obj.SanPham  = objSP;
                        DataProvider.Entities.SanPham_Bounce.Add(obj);
                    }
                }

                #endregion Thêm các Bounce của sản phẩm

                #region Thêm các Length của sản phẩm

                if (LengthId != null && LengthId.Count > 0)
                {
                    if (objSP.SanPham_LengthSize != null && objSP.SanPham_LengthSize.Count > 0)
                    {
                        DataProvider.Entities.SanPham_LengthSize.RemoveRange(objSP.SanPham_LengthSize);
                    }

                    for (int i = 0; i < LengthId.Count; i++)
                    {
                        SanPham_LengthSize obj = new SanPham_LengthSize();
                        int lengthId           = 0;
                        int.TryParse(LengthId[i], out lengthId);
                        obj.LengthId = lengthId;
                        obj.SanPham  = objSP;
                        DataProvider.Entities.SanPham_LengthSize.Add(obj);
                    }
                }

                #endregion Thêm các Length của sản phẩm

                #region Thêm các Shaft của sản phẩm

                if (ShaftId != null && ShaftId.Count > 0)
                {
                    if (objSP.SanPham_Shaft != null && objSP.SanPham_Shaft.Count > 0)
                    {
                        DataProvider.Entities.SanPham_Shaft.RemoveRange(objSP.SanPham_Shaft);
                    }

                    for (int i = 0; i < ShaftId.Count; i++)
                    {
                        SanPham_Shaft obj     = new SanPham_Shaft();
                        int           shaftId = 0;
                        int.TryParse(ShaftId[i], out shaftId);
                        obj.ShaftId = shaftId;
                        obj.SanPham = objSP;
                        DataProvider.Entities.SanPham_Shaft.Add(obj);
                    }
                }

                #endregion Thêm các Shaft của sản phẩm

                #region Thêm các Width của sản phẩm

                if (WidthId != null && WidthId.Count > 0)
                {
                    if (objSP.SanPham_Width != null && objSP.SanPham_Width.Count > 0)
                    {
                        DataProvider.Entities.SanPham_Width.RemoveRange(objSP.SanPham_Width);
                    }

                    for (int i = 0; i < WidthId.Count; i++)
                    {
                        SanPham_Width obj     = new SanPham_Width();
                        int           widthId = 0;
                        int.TryParse(WidthId[i], out widthId);
                        obj.WidthId = widthId;
                        obj.SanPham = objSP;
                        DataProvider.Entities.SanPham_Width.Add(obj);
                    }
                }

                #endregion Thêm các Width của sản phẩm

                #region Thêm các Waist của sản phẩm

                if (WaistId != null && WaistId.Count > 0)
                {
                    if (objSP.SanPham_Waist != null && objSP.SanPham_Waist.Count > 0)
                    {
                        DataProvider.Entities.SanPham_Waist.RemoveRange(objSP.SanPham_Waist);
                    }

                    for (int i = 0; i < WaistId.Count; i++)
                    {
                        SanPham_Waist obj     = new SanPham_Waist();
                        int           waistId = 0;
                        int.TryParse(WaistId[i], out waistId);
                        obj.WaistId = waistId;
                        obj.SanPham = objSP;
                        DataProvider.Entities.SanPham_Waist.Add(obj);
                    }
                }

                #endregion Thêm các Waist của sản phẩm

                #region Thêm các Inseam của sản phẩm

                if (InseamId != null && InseamId.Count > 0)
                {
                    if (objSP.SanPham_Inseam != null && objSP.SanPham_Inseam.Count > 0)
                    {
                        DataProvider.Entities.SanPham_Inseam.RemoveRange(objSP.SanPham_Inseam);
                    }

                    for (int i = 0; i < InseamId.Count; i++)
                    {
                        SanPham_Inseam obj      = new SanPham_Inseam();
                        int            inseamId = 0;
                        int.TryParse(InseamId[i], out inseamId);
                        obj.InseamId = inseamId;
                        obj.SanPham  = objSP;
                        DataProvider.Entities.SanPham_Inseam.Add(obj);
                    }
                }

                #endregion Thêm các Inseam của sản phẩm

                objSanPham.NgayCapNhat = DateTime.Now;
                objSanPham.NgayTao     = objSP.NgayTao;
                DataProvider.Entities.Entry(objSP).CurrentValues.SetValues(objSanPham);
                //Lưu lại thông tin
                DataProvider.Entities.SaveChanges();

                return(RedirectToAction("DanhSach"));
            }

            LoadDropDownList(objSanPham);

            return(View());
        }