public async Task <IActionResult> Edit(int id, [Bind("Customer,Channel,Certificate,Price,Promotion,Staff")] CertificateEditViewModel certificateEditViewModel) { if (certificateEditViewModel == null) { throw new ArgumentNullException(nameof(certificateEditViewModel)); } if (!CertificateExists(id)) { return(RedirectToAction(nameof(Edit)) .WithDanger("Update Not Successful", "Certificate Id: " + id + " doesn't exist")); } if (ModelState.IsValid) { try { Certificate certificate = await _context.Certificate.FindAsync(id); CertificateLink certificateLink = await _context.CertificateLink.FindAsync(id); Channel channel = await _context.Channel .Where(ch => ch.Id == certificateLink.ChannelId) .FirstOrDefaultAsync(); Customer customer = await _context.Customer .Where(c => c.Id == certificateLink.CustomerId) .FirstOrDefaultAsync(); Promotion promotion = await _context.Promotion .Where(p => p.Id == certificateLink.PromotionId) .FirstOrDefaultAsync(); Staff staff = await _context.Staff .Where(s => s.Id == certificateLink.StaffId) .FirstOrDefaultAsync(); certificate.ExpiryDate = certificateEditViewModel.Certificate.ExpiryDate; certificate.DateRedeemed = certificateEditViewModel.Certificate.DateRedeemed; certificate.Price = certificateEditViewModel.Certificate.Price; certificateLink.ChannelId = await _context.Channel .Where(ch => ch.ChannelName == certificateEditViewModel.Channel.ChannelName) .Select(ch => ch.Id) .FirstOrDefaultAsync(); //i believe this customerid is giving me an error on the customer name change. //so i hardcoded the query certificateLink.CustomerId = await _context.Customer .Where(c => c.Name == certificateEditViewModel.Customer.Name) .Select(c => c.Id) .FirstOrDefaultAsync(); certificateLink.PromotionId = await _context.Promotion .Where(p => p.Discount == certificateEditViewModel.Promotion.Discount) .Select(p => p.Id) .FirstOrDefaultAsync(); certificateLink.StaffId = await _context.Staff .Where(s => s.Name == certificateEditViewModel.Staff.Name) .Select(s => s.Id) .FirstOrDefaultAsync(); //if name is not is not equal to the model name if (customer.Name != certificateEditViewModel.Customer.Name) { //there is an error so i used try and catch to redirect to index try { //using fromsql func to create customer update query for name only _context.Customer.FromSql($"UPDATE dbo.customer SET dbo.customer.name = {certificateEditViewModel.Customer.Name} WHERE id = {customer.Id}").FirstOrDefault(); } catch (SqlException) { //there is an id error but it still updates customer so i used the exception like a success message return(RedirectToAction(nameof(Index)).WithSuccess("Cert#" + certificate.CertificateNo, " Successfully Updated! ")); } } _context.UpdateRange(certificate, certificateLink); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } //redirects back to customer details if origin view if (TempData["_ReturnRoute.Update.Controller"] != null) { string message = null; object _ReturnRouteController = TempData["_ReturnRoute.Update.Controller"]; TempData["_ReturnRoute.Update.Controller"] = null; if ((string)_ReturnRouteController == "Certificates" || (string)_ReturnRouteController == "Orders") { message = "Certificate #" + _context.Certificate.FindAsync(id).Result.CertificateNo + " updated successfully"; } return(Redirect("/" + _ReturnRouteController + "/" + TempData["_ReturnRoute.Update.Action"] + "/" + TempData["_ReturnRoute.Update.Id"]) .WithSuccess("Update Successful", message)); } return(RedirectToAction(nameof(Index)) .WithSuccess("Update Successful", "Certificate #" + _context.Certificate.FindAsync(id).Result.CertificateNo + " updated successfully")); } return(RedirectToAction(nameof(Index)) .WithDanger("Update Not Successful", "Something went wrong. Try again.")); }
// GET: Certificates/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } try { CertificateLink certificateLink = await _context.CertificateLink.FindAsync(id); int orderId = _context.OrderItem .Where(oi => oi.CertificateId == id).Single().OrderId; Order order = await _context.Order.FindAsync(orderId); IEnumerable <SelectListItem> staffList = from staff in await _context.Staff.ToListAsync() select new SelectListItem { Text = staff.Name, Value = staff.Name }; IEnumerable <SelectListItem> promoList = from discount in await _context.Promotion.ToListAsync() select new SelectListItem { Text = discount.Discount.ToString(), Value = discount.Discount.ToString() }; IEnumerable <SelectListItem> channelList = from channel in await _context.Channel.ToListAsync() select new SelectListItem { Text = channel.ChannelName, Value = channel.ChannelName }; IEnumerable <SelectListItem> customerList = from customer in await _context.Customer.ToListAsync() select new SelectListItem { Text = customer.Name, Value = customer.Name }; CertificateEditViewModel model = new CertificateEditViewModel() { Certificate = _context.Certificate.Find(certificateLink.CertificateId), Channel = _context.Channel.Find(certificateLink.ChannelId), Customer = _context.Customer.Find(certificateLink.CustomerId), Promotion = _context.Promotion.Find(certificateLink.PromotionId), Order = order, StaffList = staffList, PromoList = promoList, ChannelList = channelList, CustomerList = customerList }; if (model == null) { return(NotFound()); } return(View(model)); } catch (Exception) { throw; } }