public async Task <IActionResult> Edit(int id, [Bind("Id,IdMaterial,UpdateAt,Price")] PriceMaterial priceMaterial) { if (id != priceMaterial.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(priceMaterial); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PriceMaterialExists(priceMaterial.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["IdMaterial"] = new SelectList(_context.Materials, "Id", "Id", priceMaterial.IdMaterial); return(View(priceMaterial)); }
public async Task <IActionResult> Create(Material material) { var type = _context.TypeOfSupplies.Select(i => i.Name).ToList(); if (ModelState.IsValid) { //Save image to wwwroot/image if (material.ImageFile != null) { string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(material.ImageFile.FileName); string extension = Path.GetExtension(material.ImageFile.FileName); fileName = fileName + extension; material.Image = "../assets/img/" + fileName; var checkFile = Path.Combine(wwwRootPath + "/assets/img/", material.ImageFile.FileName); //Save image to wwwroot/image if (!System.IO.File.Exists(checkFile)) { string path = Path.Combine(wwwRootPath + "/assets/img/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await material.ImageFile.CopyToAsync(fileStream); } } } material.CreateAt = DateTime.Now; _context.Add(material); PriceMaterial priceMaterial = new PriceMaterial(); await _context.SaveChangesAsync(); priceMaterial.IdMaterial = material.Id; priceMaterial.UpdateAt = DateTime.Now; priceMaterial.Price = material.Price; _context.Add(priceMaterial); await _context.SaveChangesAsync(); HttpContext.Session.SetString("SuccessMessage", "Thêm mới thành công"); //History History history = new History(); history.DateHistory = DateTime.Now; history.UserName = HttpContext.Session.GetString("SessionUserName"); history.Event = "Thêm vật tư " + material.Name; _context.Add(history); await _context.SaveChangesAsync(); //============================ return(RedirectToAction(nameof(Index))); } ViewData["TypeName"] = new SelectList(_context.TypeOfSupplies, "Id", "Name", type); return(View(material)); }
public async Task <IActionResult> Create([Bind("Id,IdMaterial,UpdateAt,Price")] PriceMaterial priceMaterial) { if (ModelState.IsValid) { _context.Add(priceMaterial); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["IdMaterial"] = new SelectList(_context.Materials, "Id", "Id", priceMaterial.IdMaterial); return(View(priceMaterial)); }
public async Task <IActionResult> Edit(int?id, Material material) { if (id != material.Id) { return(NotFound()); } if (ModelState.IsValid) { try { if (material.ImageFile != null) { string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(material.ImageFile.FileName); string extension = Path.GetExtension(material.ImageFile.FileName); fileName = fileName + extension; material.Image = "../assets/img/" + fileName; var checkFile = Path.Combine(wwwRootPath + "/assets/img/", material.ImageFile.FileName); //Save image to wwwroot/image if (!System.IO.File.Exists(checkFile)) { string path = Path.Combine(wwwRootPath + "/assets/img/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await material.ImageFile.CopyToAsync(fileStream); } } } material.UpdateAt = DateTime.Now; _context.Update(material); await _context.SaveChangesAsync(); var priceCheck = _context.PriceMaterials.Where(p => p.IdMaterial == material.Id).OrderByDescending(p => p.UpdateAt).First().Price; if (priceCheck != material.Price) { PriceMaterial priceMaterial = new PriceMaterial(); priceMaterial.IdMaterial = material.Id; priceMaterial.UpdateAt = DateTime.Now; priceMaterial.Price = material.Price; _context.Add(priceMaterial); await _context.SaveChangesAsync(); } HttpContext.Session.SetString("SuccessMessage", "Cập nhật thành công"); //History History history = new History(); history.DateHistory = DateTime.Now; history.UserName = HttpContext.Session.GetString("SessionUserName"); history.Event = "Cập nhật thông tin vật tư " + material.Name; _context.Add(history); await _context.SaveChangesAsync(); //============================ } catch (DbUpdateConcurrencyException) { if (!SupplyExists(material.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } var type = _context.TypeOfSupplies.Select(i => i.Name).ToList(); ViewData["TypeName"] = new SelectList(_context.TypeOfSupplies, "Id", "Name", type); return(View(material)); }