/// <summary> /// Return View for Goods item with specific id /// </summary> /// <param name="id">id number in Goods table</param> /// <returns></returns> public ViewResult Edit(int id) { Goods goods = _repository.GetGoods() .FirstOrDefault(g => g.Id == id); AdminGoodsModel model = new AdminGoodsModel(); if (goods != null) { model.Id = goods.Id; model.AvailableCount = goods.AvailableCount; model.Name = goods.Name; model.CategoryId = goods.CategoryId; model.Color = goods.Color; model.Description = goods.Description; model.Height = goods.Height; model.Length = goods.Length; model.Width = goods.Width; model.PictrureName = goods.Pictrure; model.Price = goods.Price; } return(View(model)); }
public ActionResult Edit(AdminGoodsModel model) { if (ModelState.IsValid) { if (model.Picture != null) { // Get file name string fileName = System.IO.Path.GetFileName(model.Picture.FileName); // Saving to Content folder model.Picture.SaveAs(Server.MapPath("~/Content/Img/" + fileName)); model.PictrureName = fileName; } // Set standart image for Goods without image if (model.PictrureName == null && _repository.GetGoods().ToList().First(g => g.Id == model.Id).Pictrure == "No_image_available.png") { model.PictrureName = "No_image_available.png"; } if (model.PictrureName == null && _repository.GetGoods().ToList().First(g => g.Id == model.Id).Pictrure != null) { model.PictrureName = _repository.GetGoods().ToList().First(g => g.Id == model.Id).Pictrure; } if (model.AvailableCount < 0) { model.AvailableCount = 0; } Goods goods = _repository.GetGoods().First(g => g.Id == model.Id); goods.AvailableCount = model.AvailableCount; goods.Name = model.Name; goods.CategoryId = model.CategoryId; goods.Color = model.Color; goods.Description = model.Description; goods.Height = model.Height; goods.Length = model.Length; goods.Width = model.Width; goods.Pictrure = model.PictrureName; goods.Price = model.Price; // Saving current item and adding record to log file if (goods.Id != 0) { _repository.EditGoods(goods); } else { _repository.AddGoods(goods); } logger.Info($"User {User.Identity.Name} have changed Goods by id: {goods.Id}"); return(RedirectToAction("Index")); } else { model.PictrureName = _repository.GetGoods().First(g => g.Id == model.Id).Pictrure; return(View(model)); } }