public async Task <IActionResult> addCart(List <Item> items, int shoppingMallId, int branchId, DateTime dateSold, double totalRev) { //check to seee if the items size >0 if (items.Count > 0) { //create new cart object CartActual cart = new CartActual { createdDate = DateTime.Now, dateSold = dateSold, shoppingmallID = shoppingMallId, branchId = branchId, totalRevenue = totalRev, }; //add cart into carts context _context.CartActual.Add(cart); //save change to the new cart //note thate when the new cart object is saved //the system will automatically assign the primary key(cartId) data for each cart data row await _context.SaveChangesAsync(); //save cart first //after the operation above, cart.cartId is accessible //create a list of cart items List <CartItemActual> list1 = new List <CartItemActual>(); //loop throught each item in the items list foreach (Item item in items) { //create a new object with the structure based on Model>CartItem.cs var row = new CartItemActual { productId = item.productId, productName = item.productName, productQty = item.productQty, productPrice = item.productPrice, cartId = cart.cartId //cart Id go here }; list1.Add(row); }//end loop //assign caritem list cart.cartItems = list1; await _context.SaveChangesAsync(); //save cart first //step33: return Action to be call by client return(Json(new { newUrl = Url.Action("Index") } )); } else { return(Content("error data upload fail")); } }
public async Task <IActionResult> Create([Bind("id,name")] ShoppingMall shoppingMall) { if (ModelState.IsValid) { _context.Add(shoppingMall); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(shoppingMall)); }
public async Task <IActionResult> Create([Bind("carPlateId,carPlateNumber")] CarPlate carPlate) { if (ModelState.IsValid) { _context.Add(carPlate); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(carPlate)); }
public async Task <IActionResult> Create([Bind("branchId,branchName,shoppingmallID")] Branch branch) { if (ModelState.IsValid) { _context.Add(branch); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["shoppingmallID"] = new SelectList(_context.ShoppingMall, "id", "name", branch.shoppingmallID); return(View(branch)); }
public async Task <IActionResult> Create([Bind("id,name,code,price")] Product product) { if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); Success("create successfully", true); return(RedirectToAction(nameof(Index))); } else { Warning("Create Failed!", true); } return(View(product)); }
public async Task <IActionResult> Create(RegisterViewModel model) { if (ModelState.IsValid) { using (var transaction = _context.Database.BeginTransaction()) { try { var user = new ApplicationUser { UserName = model.Username, FirstName = model.FirstName, LastName = model.LastName, EmailConfirmed = true }; var result = await _userManager.CreateAsync(user, model.Password); // add role from role id await _userManager.AddToRoleAsync(user, model.RoleId); var data = await _userManager.Users .Where(x => x.UserName == user.UserName).FirstAsync(); if (model.RoleId == "Staff") { var createStaff = new Staff { FirstName = data.FirstName, LastName = data.LastName, ApplicationUserId = data.Id }; _context.Add(createStaff); await _context.SaveChangesAsync(); } else { var createManager = new Manager { FirstName = data.FirstName, LastName = data.LastName, ApplicationUserId = data.Id }; _context.Add(createManager); await _context.SaveChangesAsync(); } transaction.Commit(); // Success("Create Successfully!", true); return(RedirectToAction(nameof(UserController.Index))); } catch (Exception) { transaction.Rollback(); // Danger("Create Not Complete!", true); return(View(model)); } } } // Danger("Model not match!", true); return(View(model)); }