public ActionResult submitForm(Vehicle_Category category) { try { CreditWorksEntities db = new CreditWorksEntities(); bool isOverlap = overlapRange(category.RangeMin, category.RangeMax); if (isOverlap) { return(RedirectToAction("error", "Category")); } db.Vehicle_Category.Add(new Vehicle_Category() { CategoryName = category.CategoryName, CategoryIcon = category.CategoryIcon, RangeMax = category.RangeMax, RangeMin = category.RangeMin }); db.SaveChanges(); return(RedirectToAction("Create", "Category")); } catch (Exception) { throw; } }
public ActionResult Edit(int id, FormCollection formValues) { try { CreditWorksEntities db = new CreditWorksEntities(); Vehicle_Category category = db.Vehicle_Category.Where(t => t.Id == id).FirstOrDefault(); if (category == null) { return(RedirectToAction("Index", "Category")); } category.CategoryName = Request.Form["CategoryName"]; category.RangeMin = Convert.ToInt32(Request.Form["RangeMin"]); category.RangeMax = Convert.ToInt32(Request.Form["RangeMax"]); category.CategoryIcon = Request.Form["CategoryIcon"]; db.SaveChanges(); return(RedirectToAction("Index", "Category")); } catch (Exception) { throw; } }
public ActionResult Delete(int id) { try { CreditWorksEntities db = new CreditWorksEntities(); var categoryList = db.Vehicle_Category; Vehicle_Category category = categoryList.Where(t => t.Id == id).FirstOrDefault(); int count = categoryList.Count(); if (category == null) { return(RedirectToAction("Index", "Category")); } VehicleInfo vehicle = db.VehicleInfo.Where(t => t.CategoryId == id).FirstOrDefault(); //Cannot delete if categoryId has been used by VehicleInfo if (vehicle != null) { return(RedirectToAction("error", "Category")); } //Cannot delete if less than 2 category if (count <= 2) { return(RedirectToAction("Index", "Category")); } categoryList.Remove(category); db.SaveChanges(); return(RedirectToAction("Index", "Category")); } catch (Exception) { throw; } }
public ActionResult SubmitForm(VehicleInfo info) { try { CreditWorksEntities db = new CreditWorksEntities(); List <Vehicle_Category> categoryList = db.Vehicle_Category.ToList(); //use range to find categoryId foreach (var category in categoryList) { if (category.RangeMin <= info.WeightOfVehicle && category.RangeMax >= info.WeightOfVehicle) { info.CategoryId = category.Id; } } //can not be created if can not find match Category Id if (info.OwnerName != null && info.YearOfManufacture != 0 && info.WeightOfVehicle != 0 && info.CategoryId != 0) { db.VehicleInfo.Add(new VehicleInfo() { ManufacturerId = info.ManufacturerId, OwnerName = info.OwnerName, CategoryId = info.CategoryId, WeightOfVehicle = info.WeightOfVehicle, YearOfManufacture = info.YearOfManufacture }); db.SaveChanges(); } return(RedirectToAction("Create", "VehicleInfo")); } catch (Exception) { throw; } }