示例#1
0
        public async Task <IActionResult> Create([Bind("Sdtkh,DiaChiKh,Tenkh,EmailKh")] Cart cart)
        {
            if (ModelState.IsValid)
            {
                cart.NgayHD = DateTime.Now;
                cart.Id     = _context.Carts.Count() + 1;
                _context.Add(cart);
                await _context.SaveChangesAsync();



                var liscart = ListCart();
                foreach (var item in liscart)
                {
                    var TempData = new CartItem();
                    TempData.Id      = cart.Id;
                    TempData.MaTd    = item.thucDon.MaTd;
                    TempData.SoLuong = item.Quantity;
                    _context.Add(TempData);
                    await _context.SaveChangesAsync();
                }
                HttpContext.Session.Clear();
                return(RedirectToAction("category", "thucdons"));
            }
            return(View(cart));
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("MaTd,TenTd,MoTa,ImageFile,Nhom,Price")] ThucDon thucDon)
        {
            if (ModelState.IsValid)
            {
                if (thucDon.ImageFile is null)
                {
                    thucDon.Hinh = "unchose.jpg";
                }
                else
                {
                    string rootpath = _hostEnvironment.WebRootPath;
                    string fileName = Path.GetFileNameWithoutExtension(thucDon.ImageFile.FileName);
                    string ext      = Path.GetExtension(thucDon.ImageFile.FileName);
                    thucDon.Hinh = fileName = thucDon.MaTd + DateTime.Now.ToString("ddmmyyyy") + ext;
                    string path = Path.Combine(rootpath + "/img/", fileName);
                    using (var fs = new FileStream(path, FileMode.Create))
                    {
                        await thucDon.ImageFile.CopyToAsync(fs);
                    }
                }

                _context.Add(thucDon);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["Nhom"] = new SelectList(_context.Nhoms, "MaNhom", "TenNhom", thucDon.Nhom);
            return(View(thucDon));
        }
示例#3
0
        public async Task <IActionResult> Create([Bind("Id,Sdtkh,DiaChiKh,Tenkh,NgayHD,EmailKh")] Cart cart)
        {
            if (ModelState.IsValid)
            {
                _context.Add(cart);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(cart));
        }
        public async Task <IActionResult> Create([Bind("MaTd,TenTd,MoTa,Hinh,Nhom,Price")] ThucDon thucDon)
        {
            if (ModelState.IsValid)
            {
                _context.Add(thucDon);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["Nhom"] = new SelectList(_context.Nhoms, "MaNhom", "TenNhom", thucDon.Nhom);
            return(View(thucDon));
        }