public async Task <IActionResult> Create([Bind("City,Country,FirstName,LastName,Phone,PostalCode,State")] Order order) { ApplicationUser user = await _userManager.GetUserAsync(User); if (ModelState.IsValid) { ShoppingCart cart = ShoppingCart.GetCart(this.HttpContext); List <CartItem> items = cart.GetCartItems(_context); List <OrderDetail> details = new List <OrderDetail>(); foreach (CartItem item in items) { OrderDetail detail = CreateOrderDetailForThisItem(item); detail.Order = order; details.Add(detail); _context.Add(detail); } order.User = user; order.OrderDate = DateTime.Today; order.Total = ShoppingCart.GetCart(this.HttpContext).GetTotal(_context); order.Gst = (order.Total * Convert.ToDecimal(0.15)); order.OrderDetails = details; _context.SaveChanges(); return(RedirectToAction("Purchased", new RouteValueDictionary( new { action = "Purchased", id = order.OrderId }))); } return(View(order)); }
public async Task <IActionResult> Create([Bind("Id,CategroyName")] Category category) { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(category)); }
public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,HomePhoneNo,MobileNo,Email")] Supplier supplier) { if (ModelState.IsValid) { _context.Add(supplier); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(supplier)); }
public async Task <IActionResult> Create([Bind("Id,CategoryId,SupplierId,ProductName,ProductPrice, Description")] Product product, IFormFile _files) { try { if (ModelState.IsValid) { if (_files != null) { using (var memoryStream = new MemoryStream()) { await _files.CopyToAsync(memoryStream); product.ProductImage = memoryStream.ToArray(); } } else { //string path = AppDomain.CurrentDomain.BaseDirectory + "defaultBook.jpg"; // string path = @"..\wwwroot\images\Temp\defaultBook.jpg"; string path = Environment.CurrentDirectory + @"/wwwroot/images/Temp/defaultBook.jpg"; byte[] image = System.IO.File.ReadAllBytes(path); product.ProductImage = image; } _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException ex) { System.Diagnostics.Debug.WriteLine(ex); //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } PopulateCategoryDropDown(product.CategoryId); PopulateSupplierDropDown(product.SupplierId); return(View(product)); }
public async Task <IActionResult> Create([Bind("Id,CategoryId,SupplierId,ProductName,ProductPrice, Description")] Product product, IFormFile _files) { /* * var relativeName = ""; * var fileName = ""; * * if (_files.Count < 1) * { * relativeName = "/Images/logo.jpg"; * } * else * { * foreach (var file in _files) * { * fileName = ContentDispositionHeaderValue * .Parse(file.ContentDisposition) * .FileName * .Trim('"'); * //Path for localhost * relativeName = "/ProductImages/" + DateTime.Now.ToString("ddMMyyyy-HHmmssffffff") + fileName; * * using (FileStream fs = System.IO.File.Create(_hostingEnv.WebRootPath + relativeName)) * { * await file.CopyToAsync(fs); * fs.Flush(); * } * } * product.ProductImage = relativeName; * } */ try { if (ModelState.IsValid) { if (_files != null) { using (var memoryStream = new MemoryStream()) { await _files.CopyToAsync(memoryStream); product.ProductImage = memoryStream.ToArray(); } } else { //string path = AppDomain.CurrentDomain.BaseDirectory + "defaultBook.jpg"; // string path = @"..\wwwroot\images\Temp\defaultBook.jpg"; string path = Environment.CurrentDirectory + @"/wwwroot/images/Temp/defaultBook.jpg"; byte[] image = System.IO.File.ReadAllBytes(path); product.ProductImage = image; } _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException ex) { System.Diagnostics.Debug.WriteLine(ex); //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } PopulateCategoryDropDown(product.CategoryId); PopulateSupplierDropDown(product.SupplierId); return(View(product)); }