public ActionResult Create(UserDemographics obj) { using (RentMyWroxContext context = new RentMyWroxContext()) { var ids = Request.Form.GetValues("HobbyIds"); if (ids != null) { obj.Hobbies = context.Hobbies.Where(x => ids.Contains(x.Id.ToString())).ToList(); } context.UserDemographics.Add(obj); var validationErrors = context.GetValidationErrors(); if (validationErrors.Count() == 0) { context.SaveChanges(); ApplicationUser user = UserHelper.GetApplicationUser(); user.UserDemographicsId = obj.Id; context.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ServerValidationErrors = ConvertValidationErrorsToString(validationErrors); return(View("Manage", obj)); } }
public ActionResult Checkout(Order order) { Guid UserID = UserHelper.GetUserId(); ViewBag.ApplicationUser = UserHelper.GetApplicationUser(); using (RentMyWroxContext context = new RentMyWroxContext()) { var shoppingCartItems = context.ShoppingCarts .Include("Item") .Where(x => x.UserId == UserID); order.OrderDetails = new List <OrderDetail>(); order.UserId = UserID; order.OrderDate = DateTime.Now; foreach (var item in shoppingCartItems) { int quantity = 0; int.TryParse(Request.Form.Get(item.Item.Id.ToString()), out quantity); if (quantity > 0) { OrderDetail od = new OrderDetail { Item = item.Item, PricePaidEach = item.Item.Cost, Quantity = quantity }; order.OrderDetails.Add(od); } } order = context.Orders.Add(order); context.ShoppingCarts.RemoveRange(shoppingCartItems); context.SaveChanges(); return(RedirectToAction("Details", "Order", new { id = order.Id })); } }
public static void TransferTemporaryUserToRealUser(Guid tempId, string userId) { using (RentMyWroxContext context = new RentMyWroxContext()) { if (context.ShoppingCarts.Any(x => x.UserId == tempId)) { Guid newUserId = Guid.Parse(userId); var list = context.ShoppingCarts.Include("Item").Where(x => x.UserId == tempId); foreach (var tempCart in list) { var sameItemInShoppingCart = context.ShoppingCarts.FirstOrDefault(x => x.Item.Id == tempCart.Item.Id && x.UserId == newUserId); if (sameItemInShoppingCart == null) { tempCart.UserId = newUserId; } else { sameItemInShoppingCart.Quantity++; context.ShoppingCarts.Remove(tempCart); } } context.SaveChanges(); } } }
public ActionResult AddToCart(int id) { using (RentMyWroxContext context = new RentMyWroxContext()) { Item addedItem = context.Items.FirstOrDefault(x => x.Id == id); // now that we know it is a valid ID if (addedItem != null) { // Check to see if this item was already added var sameItemInShoppingCart = context.ShoppingCarts .FirstOrDefault(x => x.Item.Id == id && x.UserId == UserID); if (sameItemInShoppingCart == null) { // if not already in cart then add it ShoppingCart sc = new ShoppingCart { Item = addedItem, UserId = UserID, Quantity = 1, DateAdded = DateTime.Now }; context.ShoppingCarts.Add(sc); } else { // increment the quantity of the existing shopping cart item sameItemInShoppingCart.Quantity++; } context.SaveChanges(); } ShoppingCartSummary summary = GetShoppingCartSummary(context); return(PartialView("_ShoppingCartSummary", summary)); } }
public ActionResult Details(int id) { using (RentMyWroxContext context = new RentMyWroxContext()) { Item item = context.Items.FirstOrDefault(x => x.Id == id); UserHelper.AddUserVisit(id, context); context.SaveChanges(); return(View(item)); } }
public void DetailsView1_InsertItem() { Hobby hobby = new Hobby(); TryUpdateModel(hobby); if (ModelState.IsValid) { using (RentMyWroxContext context = new RentMyWroxContext()) { context.Hobbies.Add(hobby); context.SaveChanges(); } } }
public ActionResult Recent() { using (RentMyWroxContext context = new RentMyWroxContext()) { Guid newUserId = UserHelper.GetUserId(); var recentItems = (from uv in context.UserVisits join item in context.Items on uv.ItemId equals item.Id where uv.UserId == newUserId orderby uv.VisitDate descending select item as Item).Take(3).ToList(); context.SaveChanges(); return(PartialView("_RecentItems", recentItems)); } }
public ActionResult Edit(int id, FormCollection collection) { try { using (RentMyWroxContext context = new RentMyWroxContext()) { var item = context.UserDemographics.FirstOrDefault(x => x.Id == id); TryUpdateModel(item); context.SaveChanges(); return(RedirectToAction("Index")); } } catch { return(View()); } }
public ActionResult Edit(int id, FormCollection collection) { using (RentMyWroxContext context = new RentMyWroxContext()) { var item = context.UserDemographics.FirstOrDefault(x => x.Id == id); TryUpdateModel(item); var ids = Request.Form.GetValues("HobbyIds"); item.Hobbies = context.Hobbies.Where(x => ids.Contains(x.Id.ToString())).ToList(); var validationErrors = context.GetValidationErrors(); if (validationErrors.Count() == 0) { context.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ServerValidationErrors = ConvertValidationErrorsToString(validationErrors); return(View("Manage", item)); } }
public ActionResult Create(UserDemographics obj) { try { using (RentMyWroxContext context = new RentMyWroxContext()) { var ids = Request.Form.GetValues("HobbyIds"); obj.Hobbies = context.Hobbies .Where(x => ids.Contains(x.Id.ToString())).ToList(); context.UserDemographics.Add(obj); context.SaveChanges(); return(RedirectToAction("Index")); } } catch { return(View()); } }
protected void SaveItem_Clicked(object sender, EventArgs e) { Item item; using (RentMyWroxContext context = new RentMyWroxContext()) { if (itemId == 0) { item = new Item(); UpdateItem(item); context.Items.Add(item); } else { item = context.Items.FirstOrDefault(x => x.Id == itemId); UpdateItem(item); } context.SaveChanges(); } Response.Redirect("~/admin/ItemList"); }