public ActionResult Edit([Bind(Include = "Id,UserId,AlertType,CategoryCheckModels,NecessityValueCheckModels,Amount")] EditAlertViewModel alertViewModel) { if (!ModelState.IsValid) { return(View(alertViewModel)); } var userId = User.Identity.GetUserId(); var alert = _db.Alerts.SingleOrDefault(x => x.UserId == userId && x.Id == alertViewModel.Id); if (alert != null) { alert.CostThreshold = alertViewModel.Amount; if (alertViewModel.AlertType == AlertType.Category) { var categoryAlert = (CategoryAlert)alert; categoryAlert.CategoryIds = alertViewModel.CategoryCheckModels .Where(x => x.Checked).Select(x => x.Id).ToList(); } else { var necessityAlert = (NecessityAlert)alert; necessityAlert.NecessityValueIds = alertViewModel.NecessityValueCheckModels .Where(x => x.Checked).Select(x => x.Id).ToList(); } } _db.SaveChanges(); return(RedirectToAction("Index")); }
// GET: Alerts/Create public ActionResult Create() { var viewModel = new EditAlertViewModel { AlertTypes = GetAlertTypeSelectListItems(), CategoryCheckModels = GetCategoryCheckModels(), NecessityValueCheckModels = GetNecessityValueCheckModels() }; return(View(viewModel)); }
// GET: Alerts/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var userId = User.Identity.GetUserId(); var alert = _db.Alerts.SingleOrDefault(x => x.UserId == userId && x.Id == id); if (alert == null) { return(HttpNotFound()); } EditAlertViewModel viewModel; if (alert.AlertType == AlertType.Category) { var categoryAlert = (CategoryAlert)alert; viewModel = new EditAlertViewModel { Id = alert.Id, UserId = alert.UserId, AlertType = alert.AlertType, AlertTypes = GetAlertTypeSelectListItems(), CategoryCheckModels = GetCategoryCheckModels(categoryAlert.CategoryIds), NecessityValueCheckModels = GetNecessityValueCheckModels(), Amount = alert.CostThreshold }; } else { var necessityAlert = (NecessityAlert)alert; viewModel = new EditAlertViewModel { Id = alert.Id, UserId = alert.UserId, AlertType = alert.AlertType, AlertTypes = GetAlertTypeSelectListItems(), CategoryCheckModels = GetCategoryCheckModels(), NecessityValueCheckModels = GetNecessityValueCheckModels(necessityAlert.NecessityValueIds), Amount = alert.CostThreshold }; } return(View(viewModel)); }
public ActionResult Create([Bind(Include = "AlertType,CategoryCheckModels,NecessityValueCheckModels,Amount")] EditAlertViewModel alertViewModel) { if (!ModelState.IsValid) { return(View(alertViewModel)); } if (alertViewModel.AlertType == AlertType.Category) { var categoryAlert = new CategoryAlert { AlertType = alertViewModel.AlertType, UserId = User.Identity.GetUserId(), CategoryIds = alertViewModel.CategoryCheckModels .Where(x => x.Checked).Select(x => x.Id).ToList(), CostThreshold = alertViewModel.Amount, NumberOfMonths = 1 }; _db.Alerts.Add(categoryAlert); } else { var necessityAlert = new NecessityAlert { AlertType = alertViewModel.AlertType, UserId = User.Identity.GetUserId(), NecessityValueIds = alertViewModel.NecessityValueCheckModels .Where(x => x.Checked).Select(x => x.Id).ToList(), CostThreshold = alertViewModel.Amount, NumberOfMonths = 1 }; _db.Alerts.Add(necessityAlert); } _db.SaveChanges(); return(RedirectToAction("Index")); }