public IHttpActionResult Postsanpham(sanpham sanpham) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.sanphams.Add(sanpham); try { db.SaveChanges(); } catch { if (sanphamExists(sanpham.Id)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = sanpham.Id }, sanpham)); }
public ActionResult Create([Bind(Include = "masanpham,manhasanxuat,manhomhang,tensanpham,dongia,soluong,chitiet")] sanpham sanpham) { var fileName = ""; var path = ""; if (Request.Files.Count > 0) { var file = Request.Files[0]; if (file != null && file.ContentLength > 0) { fileName = Path.GetFileName(file.FileName); path = Path.Combine(Server.MapPath("~/Content/anh"), fileName); file.SaveAs(path); } } else { return(RedirectToAction("Adminlogin", "Admin")); } if (ModelState.IsValid) { sanpham.anhsanpham = fileName; db.sanpham.Add(sanpham); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.manhasanxuat = new SelectList(db.nhasanxuat, "manhasanxuat", "tennhasanxuat", sanpham.manhasanxuat); ViewBag.manhomhang = new SelectList(db.nhomhang, "manhomhang", "tennhomhang", sanpham.manhomhang); return(View(sanpham)); }
public ActionResult Edit(HttpPostedFileBase file, sanpham sanpham) { if (file != null) { string ImageName = System.IO.Path.GetFileName(file.FileName); if (System.IO.File.Exists("~/images/sanpham/" + sanpham.avatar)) { System.IO.File.Delete("~/images/sanpham/" + sanpham.avatar); } string physicalPath = Server.MapPath("~/images/sanpham/" + ImageName); // save image in folder file.SaveAs(physicalPath); sanpham.avatar = ImageName; } if (ModelState.IsValid) { //sanpham sanpham2 = db.sanphams.Find(sanpham.id); //sanpham.avatar = sanpham2.avatar; db.Entry(sanpham).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ID_danhmuc = new SelectList(db.Danhmucs, "id", "tenHDT", sanpham.ID_danhmuc); return(View(sanpham)); }
public int postSuasanpham(sanpham sanpham) { var kiemtratontai = db.sanphams.Where(x => x.tensp == sanpham.tensp && x.masp != sanpham.masp).ToList(); DateTime date = DateTime.Now.Date; date.ToString("yyyy-MM-dd"); if (kiemtratontai.Count > 0) { return(0); } else { if (sanpham.mota == null) { sanpham.mota = ""; } if (sanpham.hinhanh == null) { sanpham.hinhanh = ""; } sanpham.moi = 1; sanpham.ngaynhap = date; db.sanphams.AddOrUpdate(sanpham); db.SaveChanges(); return(1); } }
public ActionResult Dangnhap(TaiKhoan tk) { if (ModelState.IsValid) { MD5HASH md5Hash = new MD5HASH(); var mk = md5Hash.encrypt(tk.Matkhau); var NguoiDung = db.TaiKhoans.SingleOrDefault(x => x.Email == tk.Email && x.Matkhau == mk);//kiểm tra giá trị nhập vào if (NguoiDung != null) { Session["TaiKhoan"] = NguoiDung; Session["Admin"] = null; Session["Ten"] = NguoiDung.Ten; Session["TenDangNhap"] = NguoiDung.TenDangnhap; var giohang = db.giohangs.Where(x => x.makhachhang == NguoiDung.makh).ToList <giohang>(); List <sanpham> dsSanPham = new List <sanpham>(); foreach (var item in giohang) { sanpham sanpham = db.sanphams.Where(x => x.masp == item.masp && x.slsp >= item.soluongdamua && x.trangthai == 1).SingleOrDefault(); if (sanpham != null) { sanpham.slsp = item.soluongdamua; dsSanPham.Add(sanpham); } } if (dsSanPham.Count > 0) { Session["giohang"] = dsSanPham; } return(Redirect("/Home/Index")); } } Session["dangnhapsai"] = true; return(Redirect("/Login/Index")); }
public int postSanpham(sanpham sp) { List <sanpham> kiemtratontai = db.sanphams.Where(x => x.tensp == sp.tensp).ToList(); DateTime date = DateTime.Now.Date; date.ToString("yyyy-MM-dd"); if (kiemtratontai.Count > 0) { return(0); } else { sp.ngaynhap = date; if (sp.mota == null) { sp.mota = ""; } if (sp.hinhanh == null) { sp.hinhanh = ""; } sp.moi = 1; db.sanphams.Add(sp); return(db.SaveChanges()); } }
public void xoa(string id) { sanpham sp = (from tb in db.sanphams select tb).Single(t => t.id == id); db.sanphams.DeleteOnSubmit(sp); db.SubmitChanges(); }
public ActionResult Edit(sanpham sp) { try { if (HttpContext.Request.Files.Count > 0) { var hpf = HttpContext.Request.Files[0]; if (hpf.ContentLength > 0) { string filename = Guid.NewGuid().ToString(); string fullPathWithFileName = "/images/" + filename + ".jpg"; hpf.SaveAs(Server.MapPath(fullPathWithFileName)); sp.hinhanh = fullPathWithFileName; } } // TODO: Add update logic here //QuanLyBus.CapNhat(id,sp); var dataContext = new PetaPoco.Database("DienThoaiShopConnection"); dataContext.Update("sanpham", "MaSanPham", sp); return(RedirectToAction("Index")); } catch { return(View()); } }
public RedirectToRouteResult ThemVaoGio(int masp, int km, string kt) { if (Session["giohang"] == null) { Session["giohang"] = new List <CartItem>(); } List <CartItem> giohang = Session["giohang"] as List <CartItem>; if (giohang.FirstOrDefault(m => m.MASP == masp) == null) { ProductDao dao = new ProductDao(); sanpham sp = dao.GetItemByID(masp).FirstOrDefault(); CartItem newItem = new CartItem(); { newItem.MASP = masp; newItem.TENSP = sp.TENSP; newItem.HINHANH = sp.HINHANH; newItem.SoLuong = 1; newItem.MAKM = km; newItem.GIA = sp.GIA; newItem.KichThuoc = kt; } giohang.Add(newItem); } else { CartItem cardItem = giohang.FirstOrDefault(m => m.MASP == masp); cardItem.SoLuong++; } return(RedirectToAction("Product_Detail", "Product", new { id = masp })); }
//xóa giỏ hàng public ActionResult XoaGiohang(int ma_sp) { //kiểm tra mã sản phẩm sanpham sp = db.sanphams.SingleOrDefault(n => n.sanpham_id == ma_sp); //nếu ghép sai mã sản phẩm thì trả về trang lỗi if (sp == null) { Response.StatusCode = 404; return(null); } //lấy giỏ hàng từ session List <Giohang> listGiohang = LayGiohang(); //kiểm tra sản phẩm có tồn tại trong session Giohang gh_sp = listGiohang.SingleOrDefault(n => n.sanpham_id == ma_sp); //nếu tồn tại cho sửa if (gh_sp != null) { listGiohang.RemoveAll(n => n.sanpham_id == ma_sp); } if (listGiohang.Count == 0) { return(RedirectToAction("Index", "Home")); } return(RedirectToAction("Giohang")); }
//thêm giỏ hàng public ActionResult ThemGiohang(int ma_sp, string url) { sanpham sp = db.sanphams.SingleOrDefault(n => n.sanpham_id == ma_sp); if (sp == null) { Response.StatusCode = 404; return(null); } //khởi tạo giỏ hàng &lấy ra session giohang List <Giohang> listGiohang = LayGiohang(); //kiểm tra sách đã tồn tại trong session[Giohang] hay chưa? Giohang gh_sp = listGiohang.Find(n => n.sanpham_id == ma_sp); if (gh_sp == null) { gh_sp = new Giohang(ma_sp); //add sản phẩm mới vào list listGiohang.Add(gh_sp); return(Redirect(url)); } else { gh_sp.soluong_sp++; return(Redirect(url)); } }
//cập nhật giỏ hàng public ActionResult CapnhatGiohang(int ma_sp, FormCollection f) { //kiểm tra mã sản phẩm sanpham sp = db.sanphams.SingleOrDefault(n => n.sanpham_id == ma_sp); if (sp == null) { Response.StatusCode = 404; return(null); } //lấy giỏ hàng từ session List <Giohang> listGiohang = LayGiohang(); //kiểm tra sản phẩm có tồn tại trong session Giohang gh_sp = listGiohang.SingleOrDefault(n => n.sanpham_id == ma_sp); //nếu tồn tại cho sửa if (gh_sp != null) { gh_sp.soluong_sp = int.Parse(f["txtSoluong"].ToString()); //phut 14 } //kh gửi biến model ,chỉ gọi về nỏ nên kh trả về view //lấy giá trị từ giỏ hàng return(RedirectToAction("Giohang")); }
public ActionResult Create([Bind(Include = "id,ten,loaisanpham,ngaytao,ngaycapnhat,nguoitao,motasoluoc,motachitiet,daxoa,ghichu,hienthi,hinhdaidien,hinhanhsanpham,thutuhien,gianiemyet,giaban,mathuonghieu")] sanpham sanpham, string[] addmore) { if (Session["User"] == null) { return(RedirectToAction("Logout", "Account")); } try { if (ModelState.IsValid) { admin_account u = Session["User"] as admin_account; sanpham.nguoitao = u.id; sanpham.daxoa = false; sanpham.ngaytao = DateTime.Now; sanpham.ngaycapnhat = DateTime.Now; db.sanphams.Add(sanpham); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.loaisanpham = new SelectList(db.loaisanphams, "id", "ten", sanpham.loaisanpham); return(View(sanpham)); } catch (Exception ex) { throw ex; } }
public ActionResult Edit(int id, sanpham sp) { //try //{ // // TODO: Add update logic here // return RedirectToAction("Index"); //} //catch //{ // return View(); //} if (HttpContext.Request.Files.Count > 0) { var hpf = HttpContext.Request.Files[0]; if (hpf.ContentLength > 0) { string fileName = Guid.NewGuid().ToString(); string fullPathWithFileName = "~/images/home/" + fileName + ".jpg"; hpf.SaveAs(Server.MapPath(fullPathWithFileName)); sp.HinhAnh = fileName + ".jpg"; } } QLsanphambus.EditSanPham(id, sp); return(RedirectToAction("Index")); }
public static sanpham Update(sanpham Updated) { sanpham Item = new sanpham(); SqlParameter[] obj = new SqlParameter[20]; obj[0] = new SqlParameter("sanphamID", Updated.sanphamID); obj[1] = new SqlParameter("tensanpham", Updated.tensanpham); obj[2] = new SqlParameter("tensanphamen", Updated.tensanphamen); obj[3] = new SqlParameter("mota", Updated.mota); obj[4] = new SqlParameter("motaen", Updated.motaen); obj[5] = new SqlParameter("chitiet", Updated.chitiet); obj[6] = new SqlParameter("chitieten", Updated.chitieten); obj[7] = new SqlParameter("xuatxu", Updated.xuatxu); obj[8] = new SqlParameter("xuatxuen", Updated.xuatxuen); obj[9] = new SqlParameter("hinhanh", Updated.hinhanh); obj[10] = new SqlParameter("hinhanhlon", Updated.hinhanhlon); obj[11] = new SqlParameter("doanhnghiepID", Updated.doanhnghiepID); obj[12] = new SqlParameter("nhomID", Updated.nhomID); obj[13] = new SqlParameter("trangthai", Updated.trangthai); obj[14] = new SqlParameter("is_hot", Updated.hot); obj[15] = new SqlParameter("ngaydang", Updated.ngaydang); obj[16] = new SqlParameter("ngayhethan", Updated.ngayhethan); obj[17] = new SqlParameter("nhomconID", Updated.nhomconID); obj[18] = new SqlParameter("giaban", Updated.giaban); obj[19] = new SqlParameter("tieuchuan", Updated.tieuchuan); using (IDataReader rd = SqlHelper.ExecuteReader(DAL.con(), CommandType.StoredProcedure, "sp_sanpham_Update_UpdateNormal_linhnx", obj)) { while (rd.Read()) { Item = getFromReader(rd); } } return(Item); }
public sanpham laysanpham(int MaSP) { string qry = "select * from SanPham where MSP='" + MaSP + "'"; using (SqlConnection connect = new SqlConnection(connectionString)) { connect.Open(); SqlCommand cmd = new SqlCommand(qry, connect); SqlDataReader reader = cmd.ExecuteReader(); sanpham a = new sanpham(); if (reader.Read()) { a.MSP1 = reader.GetInt32(0); a.TenSP1 = reader.GetString(1); a.LoaiSP1 = reader.GetString(2); a.DonViTinh1 = reader.GetString(3); a.SoLuong1 = reader.GetInt32(4); a.DonGiaBan1 = reader.GetDecimal(5); a.DonGiaNhap1 = reader.GetDecimal(6); } cmd.Dispose(); reader.Dispose(); connect.Close(); return(a); } }
private void iconbtnUpdate_Click(object sender, EventArgs e) { sanpham s = new sanpham(); s.masp = txtID.Text; s.nhasx = txtNSX.Text; s.tensp = txtName.Text; s.loai = txtTypeProductName.Text; s.giaban = Int32.Parse(txtPrice.Text); s.nhacc = txtNCC.Text; ProductDAO1 p = new ProductDAO1(); int result = p.Edit(s); if (result == 1) { MessageBox.Show("Cập nhật sản phẩm thành công"); LoadData(); Clear(); } else { MessageBox.Show("Cập nhật sản phẩm thất bại"); } if ((idproduct == 0) && (name == "")) { LoadDataByResetButton(); } else { FindProduct(); } }
//[ValidateInput(false)] //thuộc tính public ActionResult Themmoi(sanpham sp, HttpPostedFileBase UrlAnh) { ViewBag.theloai_id = new SelectList(db.theloaisps.ToList(), "theloai_id", "theloai_name"); //kiểm tra đường dẫn ảnh bìa if (UrlAnh == null) { ViewBag.Thongbao = "Chọn hình ảnh"; return(View()); } //thêm vào cơ sở dữ liệu //kiểm tra sự tồn tại bên csdl if (ModelState.IsValid) { //lưu tên file var fileName = Path.GetFileName(UrlAnh.FileName); //lưu đường dẫn var path = Path.Combine(Server.MapPath("~/Hinhanh/hinhanhsp"), fileName); //kiểm tra hình ảnh đã tồn tại hay chưa if (System.IO.File.Exists(path)) { ViewBag.Thongbao = "Hình ảnh đã tồn tại"; } else { UrlAnh.SaveAs(path); } sp.hinhanh_sp = UrlAnh.FileName; db.sanphams.Add(sp); db.SaveChanges(); } return(View()); }
public ActionResult DeleteAjax(int?id, int?page, string Search) { if (Session["admin"] == null) { return(RedirectToAction("Login")); } sanpham idsp = db.sanphams.Single(n => n.MaSanPham == id); if (idsp != null) { db.sanphams.Remove(idsp); db.SaveChanges(); } int pageNumber = page ?? 1; int pageSize = 6; var sanpham = db.sanphams.ToList(); try { decimal?dg = decimal.Parse(Search); sanpham = db.sanphams.Where(n => n.DonGia <= dg).ToList(); } catch (Exception) { sanpham = db.sanphams.Where(n => n.TenSanPham.Contains(Search)).ToList(); //sp = db.sanphams.Where(n => n.ThongTin.Contains(Search)).ToList(); } return(RedirectToAction("Products", new { @page = page, @Search = Search })); }
public IHttpActionResult Putsanpham(int id, sanpham sanpham) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != sanpham.Id) { return(BadRequest()); } db.Entry(sanpham).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!sanphamExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
/// <summary> /// Thêm một sản phẩm mới vào giỏ hàng /// </summary> /// <param name="maSP">Mã sản phẩm</param> /// <returns></returns> public ActionResult ThemGioHang(int maSP) { sanpham sp = db.sanphams.Find(maSP); if (sp == null) { Response.StatusCode = 404; return(null); } List <ItemGioHang> kq = GetGioHang(); ItemGioHang getItemExists = kq.Find(n => n.MaSP == maSP); if (getItemExists != null) { if (getItemExists.SoLuong < 6 && getItemExists.SoLuong <= sp.SoLuong) { getItemExists.SoLuong++; } ViewBag.TongTien = TongTien(); ViewBag.TongSoLuong = TongSoLuong(); Session["cart"] = kq; return(PartialView("GioHangPartial")); } getItemExists = new ItemGioHang(maSP); if (getItemExists.SoLuong <= sp.SoLuong) { kq.Add(getItemExists); } ViewBag.TongTien = TongTien(); ViewBag.TongSoLuong = TongSoLuong(); Session["cart"] = kq; return(PartialView("GioHangPartial")); }
// GET: Client/Detail public ActionResult Index(int id) { DetailDao dao = new DetailDao(); sanpham sp = dao.getDetail(id); return(View(sp)); }
protected void btnThem_Click(object sender, EventArgs e) { if (Page.IsValid && FileUpload1.HasFile) { string fileName = DateTime.Now.ToString("ddMMyyyy_hhmmss_tt_") + FileUpload1.FileName; string filePath = MapPath("~/images/" + fileName); FileUpload1.SaveAs(filePath); Image1.ImageUrl = fileName; sanpham sp = new sanpham { TenSP = txtTenSP.Text, MoTa = txtMoTa.Text, HinhAnh = fileName, GiaBan = int.Parse(txtGiaBan.Text), SLCon = int.Parse(txtSLBan.Text), MaDanhMuc = int.Parse(chuyenmuc.SelectedValue.ToString()), ngayBan = DateTime.Now }; SanPhamDAO DAO = new SanPhamDAO(); if (DAO.ThemSP(sp)) { lblThongBao.Text = "Thêm sản phẩm thành công!!"; } else { lblThongBao.Text = "Thêm sản phẩm bị lỗi..."; } } }
public ActionResult Suasanpham(sanpham sanpham) { SanphamDao sanphamDao = new SanphamDao(); if (sanpham.hinhanh == "") { sanpham.hinhanh = Request["hinhanhcu"].ToString(); } int ketqua = sanphamDao.postSuasanpham(sanpham); if (ketqua > 0) { ViewData["thongbao"] = "ok"; LoaisanphamDao loaisanphamDao = new LoaisanphamDao(); ViewData["danhsachsanpham"] = sanphamDao.getSanpham((int)sanpham.masp); SelectList list = new SelectList(loaisanphamDao.getLoaisanpham().ToList(), "maloaisp", "tenloaisp"); list.Where(x => x.Text == sanphamDao.getSanpham((int)sanpham.masp).tenloaisp).First().Selected = true; ViewBag.optionslp = list; return(View()); } else { ViewData["thongbao"] = "error"; LoaisanphamDao loaisanphamDao = new LoaisanphamDao(); ViewData["danhsachsanpham"] = sanphamDao.getSanpham((int)sanpham.masp); SelectList list = new SelectList(loaisanphamDao.getLoaisanpham().ToList(), "maloaisp", "tenloaisp"); list.Where(x => x.Text == sanphamDao.getSanpham((int)sanpham.masp).tenloaisp).First().Selected = true; ViewBag.optionslp = list; return(View()); } }
//public static void CapNhat(string id, sanpham sp) //{ // var db = new DienThoaiShopConnectionDB(); // //db.Update<sanpham>("Set MaSanPham= @0, TenSanPham=@1, hinhanh=@2, Gia=@3, MaLoai=@4, NhaSX=@5, SoLuongTon=@6, MoTa=@7 where BiXoa=0", sp.MaSanPham, sp.TenSanPham, sp.hinhanh, sp.Gia, sp.MaLoai, sp.NhaSX, sp.MoTa, id); // sp = ChiTietSanPham(sp); // db.Update("sanpham", "MaSanPham", sp); //} public static void CapNhat(string id, sanpham sp) { var db = new DienThoaiShopConnectionDB(); db.Update <sanpham>("Set MaSanPham= @0, TenSanPham=@1, hinhanh=@2, Gia=@3, MaLoai=@4, NhaSX=@5, SoLuongTon=@6, MoTa=@7, BiXoa=@7", sp.MaSanPham, sp.TenSanPham, sp.hinhanh, sp.Gia, sp.MaLoai, sp.NhaSX, sp.MoTa, sp.BiXoa, id); db.Update(id, sp); }
// GET: admin/giohangs/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } TaiKhoan tk = db.TaiKhoans.Find(id); if (tk == null) { return(HttpNotFound()); } DAOADMIN listModel = new DAOADMIN(); List <muahang> listMuaHang = db.muahangs.Where(x => x.makhachhang == tk.makh).ToList <muahang>(); List <sanpham> listSanPham = new List <sanpham>(); foreach (var item in listMuaHang) { sanpham sp = db.sanphams.SingleOrDefault(x => x.masp == item.masp); sp.slsp = item.soluongdamua; listSanPham.Add(sp); } listModel.taikhoan = tk; listModel.listsanpham = listSanPham; return(View(listModel)); }
//(x) public bool capnhatsoluongton(int id, int?sl) { sanpham entity = sprp.First(c => c.id == id); entity.soluongtonkho += sl; return(sprp.Update(entity)); }
//-------------------------------------------------------quản lý sản phẩm-------------------------------------------------------------------------- public bool themsanphammoi(sanpham sp) { String qry = "SET IDENTITY_INSERT SanPham ON insert SanPham(MSP,TenSP,LoaiSP,DVT,SoLuong,DonGiaNhap,DonGiaBan) values('" + sp.MSP1 + "',N'Chưa có tên',N'Sách',N'Chưa có đơn vị tính','0' , '0','" + sp.DonGiaBan1 + "') SET IDENTITY_INSERT SanPham OFF"; return(Insert_Update_Delete(qry)); }
public ActionResult DeleteConfirmed(int id) { sanpham sanpham = db.sanphams.Find(id); db.sanphams.Remove(sanpham); db.SaveChanges(); return(RedirectToAction("Index")); }
// GET: Client/Detail public ActionResult Index(int id) { DetailDao dao = new DetailDao(); sanpham sp = dao.getDetail(id); ViewData["maloaisp"] = dao.getMaloaisanpham(id); return(View(sp)); }
public ActionResult SearchComplete(sanpham sp) { string ten = sp.tensp; if (ten != null) { using (db) { if (db.findTenSanPham(ten) != null) { ViewBag.KhongTimThay = ten; return View(db.findTenSanPham(ten).Take(16)); } else return RedirectToAction("SearchComplete", "Product"); } } return RedirectToAction("SearchComplete", "Product"); }