public ActionResult Create(MobileViewModel MobileCreate) { ViewBag.ListCategory = HttpContext.Cache[Constants.LIST_CATEGORY];//After Post, Model losses the initialized values, and it will have only values which are getting posted along with the Form i.e. input or hidden fields. Make sure you reinitialize the model before passing it to view. if (ModelState.IsValid) { var CategoryDao = new CategoryDAO(); int CategoryId = CategoryDao.GetCategoryId(MobileCreate.CategoryName); var Mobile = new Mobile { Name = MobileCreate.Name, Image = MobileCreate.Image, Price = MobileCreate.Price, Quantity = MobileCreate.Quantity, CategoryId = CategoryId, Status = MobileCreate.Status, CreateTime = DateTime.Now, }; var dao = new MobileDAO(); dao.InsertMobile(Mobile); TempData["ChangeStatus"] = "Create successful !"; } else { return(View()); } return(RedirectToAction("Index")); }
// GET: Detail public ActionResult Index(int id) { MobileDAO MobileDAO = new MobileDAO(); MobileViewModel Mobile = MobileDAO.GetMobileViewModelById(id); return(View(Mobile)); }
// GET: Admin/Mobile public ActionResult Index(int page = 1, int pageSize = 5) { var dao = new MobileDAO(); var model = dao.getListMobileViewModelPagingForAdmin(null, null, page, pageSize); return(View(model)); }
public void RemoveFromCart(int MobileId) { if (items == null) { return; } var dao = new MobileDAO(); MobileItem Mobile = dao.GetMobileItem(MobileId); if (items.ContainsKey(Mobile)) { items.Remove(Mobile); } }
public ActionResult Edit(int id) { if (HttpContext.Cache[Constants.LIST_CATEGORY] == null) { var categoryDao = new CategoryDAO(); HttpContext.Cache[Constants.LIST_CATEGORY] = categoryDao.GetListCategoryName(); } ViewBag.ListCategory = HttpContext.Cache[Constants.LIST_CATEGORY]; var dao = new MobileDAO(); var MobileViewModel = dao.GetMobileViewModelById(id); return(View(MobileViewModel)); }
// GET: Home public ActionResult Index(string searchName, string searchCategory, int page = 1, int pageSize = 9) { var dao = new MobileDAO(); var model = dao.getListMobileViewModelPaing(searchName, searchCategory, page, pageSize); var categoryDao = new CategoryDAO(); if (HttpContext.Cache[Constants.LIST_CATEGORY] == null) { HttpContext.Cache[Constants.LIST_CATEGORY] = categoryDao.GetListCategoryName(); } ViewBag.ListCategory = HttpContext.Cache[Constants.LIST_CATEGORY]; ViewBag.searchName = searchName; ViewBag.searchCategory = searchCategory; return(View(model)); }
public ActionResult Edit(MobileViewModel Mobile, double OldPrice, int OldQuantity, string OldImage) { ViewBag.ListCategory = HttpContext.Cache[Constants.LIST_CATEGORY]; var CategoryDao = new CategoryDAO(); int CategoryId = CategoryDao.GetCategoryId(Mobile.CategoryName); if (ModelState.IsValid) { if (string.IsNullOrEmpty(Mobile.Image)) { Mobile.Image = OldImage; } var dao = new MobileDAO(); dao.EditMobile(new Mobile { MobileId = Mobile.MobileId, Name = Mobile.Name, Image = Mobile.Image, Price = Mobile.Price, Quantity = Mobile.Quantity, Status = Mobile.Status, CategoryId = CategoryId }); var UpdateDao = new UpdateTableDAO(); bool check = UpdateDao.InsertUpdateTable(new UpdateTable { MobileId = Mobile.MobileId, OldPrice = OldPrice, OldQuantity = OldQuantity, NewPrice = Mobile.Price, NewQuantity = Mobile.Quantity, StatusUpdate = "Edit", Email = ((AccountLogin)Session[Constants.USER_SESSION]).Email, TimeUpdate = DateTime.Now, }); TempData["ChangeStatus"] = "Update Successful !"; } else { ModelState.AddModelError("", "Update is not Successful !"); return(View("Edit")); } return(RedirectToAction("Index")); }
public void AddToCart(int MobileId) { var dao = new MobileDAO(); MobileItem Mobile = dao.GetMobileItem(MobileId); if (items == null) { items = new Dictionary <MobileItem, int>(); } if (items.ContainsKey(Mobile)) { int quantity = items[Mobile]; items[Mobile] = ++quantity; } else { items[Mobile] = 1; } }
public ActionResult Delete(int id) { var dao = new MobileDAO(); bool check = dao.RemoveMobile(id); var UpdateDao = new UpdateTableDAO(); UpdateDao.InsertUpdateTable(new UpdateTable { MobileId = id, StatusUpdate = "Delete", Email = ((AccountLogin)Session[Constants.USER_SESSION]).Email, TimeUpdate = DateTime.Now, }); if (check) { TempData["ChangeStatus"] = "Delete Successful !"; } else { ModelState.AddModelError("", "Delete Fail !"); } return(RedirectToAction("Index")); }
public ActionResult CheckOut() { Cart cart = (Cart)Session[Constants.CART]; if (cart == null) { return(RedirectToAction("Index")); } Dictionary <MobileItem, int> items = cart.items; MobileItem[] keys = items.Keys.ToArray <MobileItem>(); MobileDAO MobileDao = new MobileDAO(); bool check = true; for (int i = 0; i < keys.Length; i++) { int quantity = MobileDao.GetQuantity(keys[i].MobileId); if (quantity == -1) { TempData["NotAvailable"] += string.Format("{0} is not available, \n", keys[i].Name); check = false; } else if (quantity < items[keys[i]]) { TempData["OutOfStock"] += string.Format("{0} is out of the stock, \n", keys[i].Name); check = false; } } if (!check) { return(RedirectToAction("Index")); } else { double?amountOfMoney = 0; for (int i = 0; i < keys.Length; i++) { amountOfMoney += keys[i].Price * items[keys[i]]; } string email = ((AccountLogin)Session[Constants.USER_SESSION]).Email; string payment = "cash"; Transaction transaction = new Transaction(); transaction.Email = email; transaction.AmountOfMoney = amountOfMoney; transaction.Payment = payment; transaction.TransactionTime = DateTime.Now; TransactionDAO transactionDAO = new TransactionDAO(); int transactionId = transactionDAO.AddTransaction(transaction); MobileDAO MobileDAO = new MobileDAO(); for (int i = 0; i < keys.Length; i++) { int MobileId = keys[i].MobileId; int quantityInDB = MobileDao.GetQuantity(MobileId); int quantityInCart = items[keys[i]]; int remain = quantityInDB - quantityInCart; MobileDAO.SetQuantity(MobileId, remain); } OrderDAO orderDAO = new OrderDAO(); for (int i = 0; i < keys.Length; i++) { Order order = new Order(); order.Quantity = items[keys[i]]; order.MobileId = keys[i].MobileId; order.MobileName = keys[i].Name; order.OrderTime = DateTime.Now; order.TransactionId = transactionId; order.Email = email; orderDAO.SaveOrder(order); } Session.Remove(Constants.CART); } return(RedirectToAction("Index")); }