public string XuLyDangNhap(string email, string password) { using (WebBanHangEntities context = new WebBanHangEntities()) { // Cach 1: Viet theo dang Method var objEmployeeLogin = context.employees .Where(p => p.email == email && p.password == password) .FirstOrDefault(); // Cach 2: Viet theo kieu LINQ //var objEmployeeLogin2 = (from p in context.employees // where p.email == email // && p.password == password // select p).FirstOrDefault(); if (objEmployeeLogin == null) { return("Khong hop le"); } else { return(String.Format("Xin chao anh {0} {1}", objEmployeeLogin.last_name, objEmployeeLogin.first_name)); } } }
public ActionResult Create(SanPham model, HttpPostedFileBase uploadedfile) { if (ModelState.IsValid) { using (WebBanHangEntities dbModel = new WebBanHangEntities()) { //string mota = Request.Form["MoTa"]; //mota = model.MoTa; string path = ""; if (uploadedfile != null && uploadedfile.ContentLength > 0) { var fileName = Path.GetFileName(uploadedfile.FileName); //path = Path.Combine(Server.MapPath("~/Content/images/products"), fileName); string pathsave = Server.MapPath("~/Content/images/products") + "/" + fileName; path = "/Content/images/products" + "/" + fileName; uploadedfile.SaveAs(pathsave); } model.HinhAnh = path; if (dbModel.SanPhams.Any(x => x.ID_SanPham == model.ID_SanPham)) { ViewBag.DuplicateMessage = "Mã sản phẩm đã tồn tại!"; return(View("Create", model)); } db.SanPhams.Add(model); db.SaveChanges(); } return(RedirectToAction("ListProduct")); } return(View(model)); }
public ActionResult Edit(int id, SanPham sp, HttpPostedFileBase uploadedfile) { try { using (WebBanHangEntities dbModel = new WebBanHangEntities()) { string path = ""; if (uploadedfile != null && uploadedfile.ContentLength > 0) { var fileName = Path.GetFileName(uploadedfile.FileName); //path = Path.Combine(Server.MapPath("~/Content/images/products"), fileName); string pathsave = Server.MapPath("~/Content/images/products") + "/" + fileName; path = "/Content/images/products" + "/" + fileName; uploadedfile.SaveAs(pathsave); } sp.HinhAnh = path; dbModel.Entry(sp).State = EntityState.Modified; dbModel.SaveChanges(); } return(RedirectToAction("ListProduct")); } catch { return(View()); } }
public ActionResult Login(KhachHang objUser) { if (ModelState.IsValid) { using (WebBanHangEntities db = new WebBanHangEntities()) { var obj = db.KhachHangs.Where(a => a.TaiKhoan.Equals(objUser.TaiKhoan) && a.MatKhau.Equals(objUser.MatKhau)).FirstOrDefault(); if (obj != null) { Session["UserID"] = obj.MaKH.ToString(); Session["UserName"] = obj.HoTen.ToString(); Session["User"] = obj.TaiKhoan.ToString(); Session["DiaChi"] = obj.DiaChi.ToString(); Session["SoDienThoai"] = obj.DienThoai.ToString(); return(RedirectToAction("Index", "Home")); } else { objUser.LoginErrorMesseger = "Tài khoản hoặc mật khẩu không đúng"; return(View("Login", objUser)); } } } return(View(objUser)); }
//Xóa sản phẩm public ActionResult Delete(int id) { using (WebBanHangEntities dbModel = new WebBanHangEntities()) { return(View(dbModel.SanPhams.Where(x => x.ID_SanPham == id).FirstOrDefault())); } }
//[ValidateAntiForgeryToken] public ActionResult DeleteProductByApi(int id) { try { using (WebBanHangEntities context = new WebBanHangEntities()) { product product = context.products.Find(id); context.products.Remove(product); context.SaveChanges(); } object result = new { Code = 200, Message = "Da xoa thanh cong!" }; return(Json(result)); } catch (Exception e) { object result = new { Code = 500, Message = "Da co loi xay ra " + e.Message }; return(Json(result)); } }
//[ValidateAntiForgeryToken] public ActionResult CreateProductByApi([FromBody] product product) { try { using (WebBanHangEntities context = new WebBanHangEntities()) { //product product = context.products.Find(id); context.products.Add(product); context.SaveChanges(); } object result = new { Code = 201, Message = "Da tao thanh cong!", CreatedObject = product }; return(Json(result)); } catch (Exception e) { object result = new { Code = 501, Message = "Da co loi xay ra " + e.Message }; return(Json(result)); } }
public ActionResult Index() { List <product> lstProduct = new List <product>(); // Lấy dữ liệu danh sách sản phẩm using (WebBanHangEntities context = new WebBanHangEntities()) // tương đương câu lệch sql: select * from products { lstProduct = context.products.ToList(); } // Truyền dữ liệu từ Controller -> View thông qua ViewBag ViewBag.DanhSachSanPham = lstProduct; return(View()); }
// GET: Product public ActionResult ProductDetail(int id) { product product = null; // Lấy dữ liệu danh sách sản phẩm using (WebBanHangEntities context = new WebBanHangEntities()) // tương đương câu lệch sql: select * from products { product = context.products.Where(p => p.id == id).FirstOrDefault(); } // Truyền dữ liệu từ Controller -> View thông qua ViewBag ViewBag.SanPham = product; return(View()); }
public ActionResult Delete(int id, FormCollection collection) { try { using (WebBanHangEntities dbModel = new WebBanHangEntities()) { SanPham sp = dbModel.SanPhams.Where(x => x.ID_SanPham == id).FirstOrDefault(); dbModel.SanPhams.Remove(sp); dbModel.SaveChanges(); } return(RedirectToAction("ListProduct")); } catch { return(View()); } }
public ActionResult DangKy(KhachHang custom) { if (ModelState.IsValid) { using (WebBanHangEntities dbModel = new WebBanHangEntities()) { if (dbModel.KhachHangs.Any(x => x.TaiKhoan == custom.TaiKhoan)) { ViewBag.DuplicateMessage = "Người dùng đã tồn tại"; return(View("DangKy", custom)); } db.KhachHangs.Add(custom); db.SaveChanges(); } ViewBag.SuccessMessage = "Đăng ký thành công!"; } return(View("DangKy", new KhachHang())); }
public System.Web.Mvc.ActionResult GetProduct() { dynamic lstProduct = null; using (WebBanHangEntities context = new WebBanHangEntities()) { //lstProduct = context.products.ToList(); lstProduct = (from p in context.products select new { p.id, p.product_code, p.product_name, p.standard_cost, p.list_price }).ToList(); } return(Json(lstProduct, JsonRequestBehavior.AllowGet)); }
public System.Web.Mvc.ActionResult GetCustomers() { dynamic lstCustomer = null; using (WebBanHangEntities context = new WebBanHangEntities()) { //lstProduct = context.products.ToList(); lstCustomer = (from p in context.customers select new { p.id, p.first_name, p.last_name, p.address1, p.address2, p.email, p.phone }).ToList(); } return(Json(lstCustomer, JsonRequestBehavior.AllowGet)); }
public System.Web.Mvc.ActionResult GetOrders() { dynamic lstOrder = null; using (WebBanHangEntities context = new WebBanHangEntities()) { //lstProduct = context.products.ToList(); lstOrder = (from o in context.orders join c in context.customers on o.customer_id equals c.id select new { orderid = o.id, customerid = c.id, c.first_name, c.last_name, c.address1, c.address2, c.email, c.phone }).ToList(); } return(Json(lstOrder, JsonRequestBehavior.AllowGet)); }
public BaseDAO() { Model = new WebBanHangEntities(); }
public string XuLyDangKy(string last_name, string first_name, string email , string password, HttpPostedFileBase avatar, string job_title, string department, int manager_id, string phone, string address1, string address2, string city, string state, string postal_code, string country) { try { string _FileName = ""; string datetimeFolderName = ""; if (avatar.ContentLength > 0) { _FileName = Path.GetFileName(avatar.FileName); string _FileNameExtension = Path.GetExtension(avatar.FileName); if ((_FileNameExtension == ".png" || _FileNameExtension == ".jpg" || _FileNameExtension == ".jpeg" || _FileNameExtension == ".svg" || _FileNameExtension == ".xlsx" || _FileNameExtension == ".docx" ) == false) { return(String.Format("File co duoi {0} khong duoc chap nhan, vui long kiem tra lai", _FileNameExtension)); } DateTime now = DateTime.Now; datetimeFolderName = String.Format("{0}{1}{2}{3}{4}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); string uploadFolderPath = Server.MapPath("~/UploadedFiles/" + datetimeFolderName); if (Directory.Exists(uploadFolderPath) == false) { Directory.CreateDirectory(uploadFolderPath); } string _Path = Path.Combine(uploadFolderPath, _FileName); avatar.SaveAs(_Path); } using (WebBanHangEntities context = new WebBanHangEntities()) { //Tao 1 dong moi nhung chua luu vao table cua Database employee newRow = new employee(); newRow.last_name = last_name; newRow.first_name = first_name; newRow.email = email; newRow.password = password; //newRow.avatar = avatar; newRow.avatar = datetimeFolderName + "/" + _FileName; newRow.job_title = job_title; newRow.department = department; newRow.manager_id = manager_id; newRow.phone = phone; newRow.address1 = address1; newRow.address2 = address2; newRow.city = city; newRow.state = state; newRow.postal_code = postal_code; newRow.country = country; // Sinh cau lenh de luu du lieu vao table context.employees.Add(newRow); // Thuc hien cau lenh de luu context.SaveChanges(); return(String.Format("Tai khoan {0} {1} da duoc tao", last_name, first_name)); } } catch (Exception ex) { return(String.Format("Co loi xay ra, thong tin loi {0} {1}", last_name, first_name)); } }
//[ValidateAntiForgeryToken] public ActionResult EditeProductByApi(int id, [FromBody] product product) { try { using (WebBanHangEntities context = new WebBanHangEntities()) { product productEdit = context.products.Find(id); if (String.IsNullOrEmpty(product.product_code)) { productEdit.product_code = product.product_code; } if (String.IsNullOrEmpty(product.product_name)) { productEdit.product_name = product.product_name; } if (String.IsNullOrEmpty(product.description)) { productEdit.description = product.description; } if (product.standard_cost > 0) { productEdit.standard_cost = product.standard_cost; } if (product.list_price > 0) { productEdit.list_price = product.list_price; } if (product.target_level > 0) { productEdit.target_level = product.target_level; } if (product.minimum_reorder_quantity > 0) { productEdit.minimum_reorder_quantity = product.minimum_reorder_quantity; } if (String.IsNullOrEmpty(product.quantity_per_unit)) { productEdit.quantity_per_unit = product.quantity_per_unit; } if (product.discontinued > 0) { productEdit.discontinued = product.discontinued; } if (String.IsNullOrEmpty(product.category)) { productEdit.category = product.category; } if (String.IsNullOrEmpty(product.image)) { productEdit.image = product.image; } context.SaveChanges(); } object result = new { Code = 202, Message = "Da thay doi thong tin thanh cong!" }; return(Json(result)); } catch (Exception e) { object result = new { Code = 502, Message = "Da co loi xay ra " + e.Message }; return(Json(result)); } }