public ActionResult Add(HomeViewModel model) { if (model == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var userGraph = db.Users.Where(u => u.Email == User.Identity.Name).FirstOrDefault(); if (userGraph != null) { var vehicleMake = db.Makes.Where(v => v.ID == model.SelectedVehicle).FirstOrDefault(); var newVehicle = new Vehicle() { Make = vehicleMake, MPG = model.SelectedVehicleMPG }; userGraph.Vehicles.Add(newVehicle); db.SaveChanges(); } return RedirectToAction("Index"); }
public ActionResult Edit(HomeViewModel model) { if (ModelState.IsValid) { var userGraph = db.Users.Where(u => u.Email == User.Identity.Name).FirstOrDefault(); if (userGraph != null) { foreach (var updatedVehicle in model.MyVehicles) { foreach (var v in userGraph.Vehicles) { if (v.ID == updatedVehicle.ID) v.MPG = updatedVehicle.MPG; } } db.SaveChanges(); } return RedirectToAction("Index"); } return View(); }
public ActionResult Index() { var model = new HomeViewModel(); var userGraph = db.Users.Where(u => u.Email == User.Identity.Name).FirstOrDefault(); if (userGraph != null) { model.MyVehicles = userGraph.Vehicles; } var list = db.Makes.ToList(); var dropDown = new List<SelectListItem>(); foreach (var i in list) { dropDown.Add(new SelectListItem { Text = i.Name, Value = i.ID.ToString() }); } model.AllMakes = dropDown; return View(model); }