public ActionResult CreateModel(int manuId, ModelofDevice created) { if (_db.Manufacturers.FirstOrDefault(manu => manu.ManufacturerId == manuId) == null) { return(RedirectToAction("")); } //should either add a viewbag or model parameter to pass manufaturername to view if (!ModelState.IsValid) { return(View(created)); } //check to see if name is taken only for this manufacturer, use all lower case sovariation on capitals is irrelevant var nameExists = _db.DeviceModels.Where(mod => mod.ManufacturerId == manuId) .Any(mod => mod.Model.ToLower() == created.Model.ToLower()); if (nameExists) { ModelState.AddModelError("", "This Model already exists in the Records!"); return(View(created)); } _db.DeviceModels.Add(created); _db.SaveChanges(); return(RedirectToAction("")); }
public ActionResult CreateModel(int manuId) { if (_db.Manufacturers.FirstOrDefault(manu => manu.ManufacturerId == manuId) == null) { return(RedirectToAction("")); } var toAdd = new ModelofDevice { ManufacturerId = manuId }; return(View(toAdd)); }
public ActionResult EditModel(int modId, ModelofDevice edited) { var unedited = _db.DeviceModels.FirstOrDefault(mod => mod.Identifier == modId); if (unedited == null) { return(RedirectToAction("")); } if (!ModelState.IsValid) { return(View(edited)); } //check if name exists in db var nameExists = _db.DeviceModels.Any(mod => mod.Model == edited.Model); if (nameExists) { //if name exists check to see whether the name is the same id as the one being edited var namedModel = _db.DeviceModels.FirstOrDefault(mod => mod.Model == edited.Model); if (namedModel.Identifier != modId) { ModelState.AddModelError("", "Name already exists within records!"); return(View(edited)); } else { //nothing was changed in this case return(RedirectToAction("")); } } unedited.Model = edited.Model; _db.SaveChanges(); return(RedirectToAction("")); }