namespace books.BL { public class ClsItem { public StoreBooksContext booksContext=new StoreBooksContext(); public List<tbitem> GetAll() { List<tbitem> lstItems = booksContext.TbItems. OrderByDescending(a => a.ItemName). ThenByDescending(a => a.SalePrice).ToList(); return lstItems; } public TbItem GetById(int id) { TbItem SelectedItem = booksContext.TbItems.FirstOrDefault(i => i.ItemId == id); return SelectedItem; } public bool AddToDataBase(TbItem item) { try { booksContext.TbItems.Add(item); booksContext.SaveChanges(); return true; } catch (Exception) { throw; } } public bool Edit(TbItem item) { try { booksContext.Entry(item).State = EntityState.Modified; booksContext.SaveChanges(); return true; } catch (Exception) { throw; } } } }
namespace books.Areas.Admin.Controllers { [Area("Admin")] public class ItemsController : Controller { public ClsItem clsItem = new ClsItem(); public IActionResult List() { return View(clsItem.GetAll()); } public IActionResult Edit(int? id) { ClsCategories clsCategories = new ClsCategories(); ViewBag.clsCategoryList = clsCategories.GetAll(); if (id != null) { return View(clsItem.GetById(Convert.ToInt32(id))); } else { return View(); } } [HttpPost] public async Task <iactionresult> Save(TbItem item,List<iformfile> Files) { try { if (ModelState.IsValid) { foreach (var file in Files) { if (file != null) { string ImageName = Guid.NewGuid().ToString() + ".jpg"; var filePaths = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Uploads", ImageName); using (var stream = System.IO.File.Create(filePaths)) { await file.CopyToAsync(stream); } item.ImageName = ImageName; } } if (item.ItemId == 0) clsItem.AddToDataBase(item); else clsItem.Edit(item); return RedirectToAction("List", "Items"); } return RedirectToAction("List", "Items"); } catch (Exception ex) { throw ex; } } } }
catch (Exception ex) { throw ex; }
throw;
throw ex;
ClsItem
catch
try..catch
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)