public bool SaveOrder() { using (var db = new RepositoryNS.Repository()) { using (var dbContextTransaction = db.Context.Database.BeginTransaction()) { try { List<Order> orders = new List<Order>(); if (Session["Cart"] != null) { orders = (List<Order>)Session["Cart"]; foreach (var or in orders) db.Add(or); } db.Save(); dbContextTransaction.Commit(); return true; } catch (Exception ex) { dbContextTransaction.Rollback(); return false; } } } }
public ActionResult AddToCard(string DateStart, string DateEnd) { try { List<Order> orders = new List<Order>(); var carAmount = 0; var carName = ""; using (var db = new RepositoryNS.Repository()) { var carID = ((Order)Session["tempOrder"]).CarId; carAmount = db.Context.Car.Where(x => x.Id == carID).Select(x => x.BaseAmount).FirstOrDefault(); carName = db.Context.Car.Where(x => x.Id == carID).Select(x => x.Model ).FirstOrDefault(); } var order = new Order(); order.UserId = ((User)Session["user"]).Id; order.CarId = ((Order)Session["tempOrder"]).CarId; order.DateStart = DateTime.Parse(DateStart); order.DateEnd = DateTime.Parse(DateEnd); TimeSpan ts = (DateTime)order.DateEnd - (DateTime)order.DateStart; order.CassierId = 1; order.Amount = GetAmount(carAmount, false, ts.TotalHours); using (var db = new RepositoryNS.Repository()) { db.Add(order); db.Save(); } if (Session["Cart"] != null) { orders = (List<Order>)Session["Cart"]; orders.Add(order); } else { orders.Add(order); } Session["Cart"] = orders; return Redirect("/CarsCatalog"); } catch (Exception ex) { return RedirectToAction("/CarsCatalog"); } }