Exemplo n.º 1
0
        public ActionResult Index(LoginClientModel model)
        {
            var dao            = new NguoiDungDao();
            var ketquadangnhap = dao.LoginClient(model.tenDangNhap, model.matKhau);

            if (ModelState.IsValid)
            {
                if (ketquadangnhap)
                {
                    var nguoidung        = dao.getByMaNguoiDung(model.tenDangNhap);
                    var nguoidungSession = new NguoiDungLogin();
                    nguoidungSession.tendangnhap = nguoidung.TENDANGNHAP;
                    nguoidungSession.idnguoidung = nguoidung.IDNGUOIDUNG;
                    Session.Add(CommonConstant.USER_SESSION, nguoidungSession);
                    return(RedirectToAction("Index", "TrangChu"));
                }
                else
                {
                    ModelState.AddModelError("", "Tên đăng nhập hoặc mật khẩu không đúng");
                }
            }
            else
            {
                ModelState.AddModelError("", "Bạn cần nhập thông tin đầy đủ");
            }
            return(View("Index"));
        }
Exemplo n.º 2
0
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                var dao = new NguoiDungDao();
                if (model.Email == "ad1")
                {
                    return(RedirectToAction("Index", "SanPham", new { area = "Admin" }));
                }
                var result = dao.Login(model.Email, model.Pass);
                if (result)
                {
                    var NguoiDung        = dao.GetById(model.Email);
                    var NguoiDungSession = new NguoiDungLogin();
                    NguoiDungSession.Email    = NguoiDung.Email;
                    NguoiDungSession.ID       = NguoiDung.MaND;
                    NguoiDungSession.UserName = NguoiDung.UserName;


                    Session.Add(CommomConstants.NguoiDungSession, NguoiDungSession);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Mật khẩu hoặc email không đúng.");
                }
            }
            return(View("Index"));
        }
Exemplo n.º 3
0
        public ActionResult NguoiDungDetail()
        {
            var sessnd = Session[CommomConstants.NguoiDungSession];
            var nd     = new NguoiDungLogin();

            nd = (NguoiDungLogin)sessnd;
            return(View(nd));
        }
Exemplo n.º 4
0
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                DBModels db = new DBModels();

                var dao    = new NguoiDungDao();
                var result = dao.Login(model.Email, model.Pass);
                if (result)
                {
                    var NguoiDung        = dao.GetById(model.Email);
                    var NguoiDungSession = new NguoiDungLogin();
                    NguoiDungSession.Email    = NguoiDung.Email;
                    NguoiDungSession.ID       = NguoiDung.MaND;
                    NguoiDungSession.UserName = NguoiDung.UserName;
                    var list = new List <CartItem>();

                    // kiểm tra giỏ hàng trong db
                    GioHang gh = db.GioHangs.SingleOrDefault(x => x.MaND == NguoiDung.MaND); // lấy giỏ hàng của người dùng hiện tại

                    if (gh == null)
                    {
                        gh      = new GioHang();
                        gh.MaGH = NguoiDungSession.ID;
                        while (gh.MaGH.Length > 10)
                        {
                            gh.MaGH = gh.MaGH.Substring(1, gh.MaGH.Length - 2);
                        }
                        gh.MaND = NguoiDung.MaND;
                        db.GioHangs.Add(gh);
                        db.SaveChanges();
                    }
                    //else
                    //{
                    //    foreach (var item in db.CTGioHangs)
                    //    {
                    //        if (item.MaGH == gh.MaGH)
                    //        {
                    //            var t = new CartItem();
                    //            //var SanPham = new SanPhamDAO().ViewDetail(item.MaSP);
                    //            t.SanPham = item.SanPham;
                    //            t.Quantity = (int)item.SoLuong;
                    //            list.Add(t);
                    //        }
                    //    }
                    //}

                    Session.Add(CommomConstants.NguoiDungSession, NguoiDungSession);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Mật khẩu hoặc email không đúng.");
                }
            }
            return(View("Index"));
        }
Exemplo n.º 5
0
        public ActionResult Create(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                var dao = new NguoiDungDao();

                NguoiDung nd = new NguoiDung();
                nd.TenND    = model.TenND;
                nd.Email    = model.Email;
                nd.Pass     = model.Pass;
                nd.UserName = model.UserName;
                nd.MaND     = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
                while (nd.MaND.Length > 10)
                {
                    nd.MaND = nd.MaND.Substring(1, nd.MaND.Length - 2);
                }
                nd.MaLoaiND = "lnd2";
                NguoiDung kt = dao.ViewDetail(nd.MaND);
                if (kt != null)
                {
                    ModelState.AddModelError("", "Người dùng đã tồn tại!");
                }
                else
                {
                    NguoiDung kte = dao.GetById(nd.Email);
                    if (kte != null)
                    {
                        ModelState.AddModelError("", "Email đã đăng ký!");
                    }
                    else
                    {
                        bool id = dao.Insert(nd);
                        if (id)
                        {
                            var NguoiDungSession = new NguoiDungLogin();
                            NguoiDungSession.Email    = nd.Email;
                            NguoiDungSession.ID       = nd.MaND;
                            NguoiDungSession.UserName = nd.UserName;


                            Session.Add(CommomConstants.NguoiDungSession, NguoiDungSession);
                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            ModelState.AddModelError("", "Tạo mới người dùng thất bại!");
                        }
                    }
                }
            }
            return(View("Index"));
        }
Exemplo n.º 6
0
        // GET: Cart
        public ActionResult Index()
        {
            //lấy về giỏ hàng hiện tại
            var      cart = Session[CommomConstants.CartSession];
            var      list = new List <CartItem>();
            DBModels db   = new DBModels();

            // lấy về người đăng nhập hiện tại
            var sessnd = Session[CommomConstants.NguoiDungSession];
            var nd     = new NguoiDungLogin();

            nd = (NguoiDungLogin)sessnd;

            // nếu chưa đăng nhập
            if (sessnd == null)
            {
                if (cart != null)
                {
                    list = (List <CartItem>)cart;
                }
                ViewBag.idgh = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString(); // tạo id tạm dựa trên thời gian
                while (ViewBag.idgh.Length > 10)
                {
                    ViewBag.idgh = ViewBag.idgh.Substring(1, ViewBag.idgh.Length - 2);
                }
            }
            else // đã đăng nhập
            {
                // kiểm tra giỏ hàng trong db
                var gh = new GioHang();
                gh = db.GioHangs.SingleOrDefault(x => x.MaND == nd.ID); // lấy giỏ hàng của người dùng hiện tại

                //đưa giỏ hàng trong db vào sess cart
                foreach (var item in db.CTGioHangs)
                {
                    if (item.MaGH == gh.MaGH)
                    {
                        var t = new CartItem();
                        //var SanPham = new SanPhamDAO().ViewDetail(item.MaSP);
                        t.SanPham  = item.SanPham;
                        t.Quantity = (int)item.SoLuong;
                        list.Add(t);
                    }
                }
                ViewBag.idgh = gh.MaGH;
            }
            Session[CommomConstants.CartSession] = list;
            return(View(list));
        }
        // GET: PhieuXuat
        public ActionResult Index(int?page)
        {
            // 1. Tham số int? dùng để thể hiện null và kiểu int
            // page có thể có giá trị là null và kiểu int.

            // lấy về người đăng nhập hiện tại
            var sessnd = Session[CommomConstants.NguoiDungSession];
            var nd     = new NguoiDungLogin();

            nd = (NguoiDungLogin)sessnd;

            DBModels         db   = new DBModels();
            List <PhieuXuat> list = new List <PhieuXuat>();

            foreach (var item in db.PhieuXuats)
            {
                if (item.MaND == nd.ID)
                {
                    list.Add(item);
                }
            }
            // 2. Nếu page = null thì đặt lại là 1.
            if (page == null)
            {
                page = 1;
            }

            // 3. Tạo truy vấn, lưu ý phải sắp xếp theo trường nào đó, ví dụ OrderBy
            // theo LinkID mới có thể phân trang.
            var links = (from l in list select l).OrderBy(x => x.MaPX);

            // 4. Tạo kích thước trang (pageSize) hay là số Link hiển thị trên 1 trang
            int pageSize = 5;

            // 4.1 Toán tử ?? trong C# mô tả nếu page khác null thì lấy giá trị page, còn
            // nếu page = null thì lấy giá trị 1 cho biến pageNumber.
            int pageNumber = (page ?? 1);

            ViewBag.lsp = db.LoaiSPs;
            ViewBag.ncc = db.NhaCCs;

            // 5. Trả về các Link được phân trang theo kích thước và số trang.
            return(View(links.ToPagedList(pageNumber, pageSize)));
        }
Exemplo n.º 8
0
        public ActionResult Delete(string id)
        {
            while (id.Length < 10)
            {
                id += " ";
            }
            var cart = Session[CommomConstants.CartSession];
            var list = (List <CartItem>)cart;
            // lấy về người đăng nhập hiện tại
            var sessnd = Session[CommomConstants.NguoiDungSession];
            var nd     = new NguoiDungLogin();

            nd = (NguoiDungLogin)sessnd;

            // nếu chưa đăng nhập
            if (sessnd == null)
            {
                foreach (var item in list.ToList())
                {
                    if (item.SanPham.MaSP == id) //kiểm tra id của sản phẩm trong giỏ hàng trùng với id chuyền vào => xóa sản phẩm
                    {
                        list.Remove(item);
                    }
                }

                Session[CommomConstants.CartSession] = list;
            }
            else
            {
                DBModels db = new DBModels();
                // kiểm tra giỏ hàng trong db
                var gh = new GioHang();
                gh = db.GioHangs.SingleOrDefault(x => x.MaND == nd.ID); // lấy giỏ hàng của người dùng hiện tại

                var item = db.CTGioHangs.Find(gh.MaGH, id);
                db.CTGioHangs.Remove(item);
                db.SaveChanges();
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 9
0
 public ActionResult Creates(NGUOIDUNG model)
 {
     try
     {
         // TODO: Add update logic here
         if (ModelState.IsValid)
         {
             var ketqua = new NguoiDungDao().ThemNguoiDung(model);
             if (ketqua)
             {
                 var timlai = db.NGUOIDUNGs.Find(model.IDNGUOIDUNG);
                 timlai.TRANGTHAI = true;
                 timlai.DOTINCAY  = 100;
                 db.SaveChanges();
                 var dao            = new NguoiDungDao();
                 var ketquadangnhap = dao.LoginClient(model.TENDANGNHAP, model.MATKHAU);
                 if (ketquadangnhap)
                 {
                     var nguoidungsession = new NguoiDungLogin();
                     nguoidungsession.idnguoidung = model.IDNGUOIDUNG;
                     nguoidungsession.tendangnhap = model.TENDANGNHAP;
                     Session.Add(CommonConstant.USER_SESSION, nguoidungsession);
                     return(RedirectToAction("Index", "TrangChu"));
                 }
             }
             else
             {
                 return(View("Error_404"));
             }
         }
     }
     catch
     {
         return(View("Error_404"));
     }
     return(View("Index"));
 }
Exemplo n.º 10
0
        public ActionResult AddItem(string id, int quantity)
        {
            var SanPham = new SanPhamDAO().ViewDetail(id);
            var cart    = Session[CommomConstants.CartSession];

            // lấy về người đăng nhập hiện tại
            var sessnd = Session[CommomConstants.NguoiDungSession];
            var nd     = new NguoiDungLogin();

            nd = (NguoiDungLogin)sessnd;

            // nếu chưa đăng nhập
            if (sessnd == null)
            {
                if (cart != null)
                {
                    var list = (List <CartItem>)cart;           //ép kiểu về list cartitem lấy từ session(dữ liệu trong session là kiểu list
                    if (list.Exists(x => x.SanPham.MaSP == id)) //nếu list chứa productid truyền vào
                    {
                        foreach (var item in list)
                        {
                            if (item.SanPham.MaSP == id) // nếu đã tồn tại id mà thêm một id => tăng thêm số lượng
                            {
                                item.Quantity += quantity;
                            }
                        }
                    }
                    else // add mới một sản phẩm
                    {
                        //tạo mới đối tượng cart item
                        var item = new CartItem();
                        item.SanPham  = SanPham;
                        item.Quantity = quantity;
                        list.Add(item);
                    }
                    //Gán vào session
                    Session[CommomConstants.CartSession] = list;
                }
                else
                {
                    //tạo mới đối tượng cart item
                    var item = new CartItem();
                    item.SanPham  = SanPham;
                    item.Quantity = quantity;
                    var list = new List <CartItem>();
                    list.Add(item);
                    //Gán vào Session
                    Session[CommomConstants.CartSession] = list;
                }
            }
            else //đã đăng nhập
            {
                DBModels db = new DBModels();
                // kiểm tra giỏ hàng trong db
                var gh = new GioHang();
                gh = db.GioHangs.SingleOrDefault(x => x.MaND == nd.ID); // lấy giỏ hàng của người dùng hiện tại

                var item = db.CTGioHangs.Find(gh.MaGH, SanPham.MaSP);
                if (item != null)
                {
                    item.SoLuong += quantity;
                    db.SaveChanges();
                }
                else
                {
                    var tem = new CTGioHang();
                    tem.MaGH    = gh.MaGH;
                    tem.MaSP    = SanPham.MaSP;
                    tem.SoLuong = quantity;
                    db.CTGioHangs.Add(tem);
                    db.SaveChanges();
                }
            }


            return(RedirectToAction("Index"));
        }
Exemplo n.º 11
0
        public ActionResult Check()
        {
            //lấy về giỏ hàng hiện tại
            var cart = Session[CommomConstants.CartSession];
            var list = new List <CartItem>();

            list = (List <CartItem>)cart;
            DBModels db = new DBModels();

            // lấy về người đăng nhập hiện tại
            var sessnd = Session[CommomConstants.NguoiDungSession];
            var nd     = new NguoiDungLogin();

            nd = (NguoiDungLogin)sessnd;

            if (nd == null)
            {
                var px = new PhieuXuat();
                px.MaND = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
                while (px.MaND.Length > 10)
                {
                    px.MaND = px.MaND.Substring(1, px.MaND.Length - 2);
                }
                px.MaPX     = px.MaND;
                px.NgayDat  = DateTime.Now;
                px.NgayShip = DateTime.Now;
                db.PhieuXuats.Add(px);

                var kvl = new NguoiDung();
                kvl.MaND = px.MaND;
                db.NguoiDungs.Add(kvl);



                db.SaveChanges();
                //Gán vào session
                Session[CommomConstants.CartSession] = null;
                return(RedirectToAction("Success", "Card"));
            }
            else
            {
                var px = new PhieuXuat();
                px.MaPX = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
                while (px.MaPX.Length > 10)
                {
                    px.MaPX = px.MaPX.Substring(1, px.MaPX.Length - 2);
                }
                px.MaND     = nd.ID;
                px.NgayDat  = DateTime.Now.Date;
                px.NgayShip = DateTime.Now.Date;
                db.PhieuXuats.Add(px);

                foreach (var i in list)                 //list cac san pham trong gio hang
                {
                    db.CTPhieuXuats.Add(new CTPhieuXuat //them tuong ung vao phieu xuat
                    {
                        MaPX    = px.MaPX,
                        MaSP    = i.SanPham.MaSP,
                        SoLuong = i.Quantity
                    });
                }
                //list.Clear();
                db.SaveChanges();
                //Gán vào session
                Session[CommomConstants.CartSession] = list;
                return(RedirectToAction("Success", "Card"));
            }
            //return RedirectToAction("Index", "Card");
        }