public IActionResult SaveImage(SaveImageViewModel model) { Image image = _ctx.Images .FirstOrDefault(x => x.Id == model.Id); image.Category = _ctx.Categories .FirstOrDefault(x => x.Id == model.CategoryId); image.FileName = model.FileName; image.ImageUrl = model.ImageUrl; image.CategoryId = model.CategoryId; var vm = new ImageVieModel(); { vm.CategoryName = image.Category.Name; vm.FileName = image.FileName + new Guid(); vm.ImageUrl = image.ImageUrl; } try { _ctx.Entry(image).State = EntityState.Modified; _ctx.SaveChanges(); return(Ok(vm)); } catch (Exception ex) { return(BadRequest(ex)); } }
public async Task <IHttpActionResult> PutTrip(int id, Trip trip) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != trip.Id) { return(BadRequest()); } db.Entry(trip).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TripExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Edit(Course course) { if (ModelState.IsValid) { var courseToUpdate = db.Courses.Include(i => i.Students).First(i => i.Id == course.Id); courseToUpdate.Name = course.Name; foreach (var student in db.Students) { if (!course.SelectedStudents.Contains(student.Id)) { courseToUpdate.Students.Remove(student); } else { courseToUpdate.Students.Add((student)); } } db.Entry(courseToUpdate).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(course)); }
public IHttpActionResult PutCarguide(int id, Carguide carguide) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != carguide.CarguideId) { return(BadRequest()); } db.Entry(carguide).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CarguideExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Edit(TimeEntryViewModel timeEntryViewModel) { if (timeEntryViewModel.Id > 0) { try { //var timeEntyviewModel = new TimeEntryViewModel(); var timeEntryToUpdate = db.TimeEntries.First(i => i.Id == timeEntryViewModel.Id); timeEntryToUpdate.Hours = timeEntryViewModel.Hours; timeEntryToUpdate.Description = timeEntryViewModel.Description; timeEntryToUpdate.Date = timeEntryViewModel.Date; timeEntryToUpdate.Project = timeEntryViewModel.Project; db.Entry(timeEntryToUpdate.Project).State = EntityState.Unchanged; db.Entry(timeEntryToUpdate).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } } try { var timeEntryToSave = new TimeEntry(); timeEntryToSave.Hours = timeEntryViewModel.Hours; timeEntryToSave.Description = timeEntryViewModel.Description; timeEntryToSave.Date = timeEntryViewModel.Date; timeEntryToSave.Project = timeEntryViewModel.Project; db.TimeEntries.Add(timeEntryToSave); db.Entry(timeEntryToSave.Project).State = EntityState.Unchanged; db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Edit([Bind(Include = "Id,Description,ProjectOnwerId,Created,Active")] ToDo toDo) { if (ModelState.IsValid) { db.Entry(toDo).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(toDo)); }
public ActionResult Edit([Bind(Include = "Id,FirstName,Lastname")] Student student) { if (ModelState.IsValid) { db.Entry(student).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(student)); }
public ActionResult Edit(Album album) { if (ModelState.IsValid) { db.Entry(album).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); return(View(album)); }
public string Post(Student student) { if (student.Id > 0) //Save it { db.Entry(student).State = System.Data.Entity.EntityState.Modified; } else // Add it { db.Students.Add(student); } db.SaveChanges(); return(string.Format("{0} {1}", student.FirstName, student.LastName)); }
public string Post(Student student) { if (student.Id > 0) // save { db.Entry(student).State = System.Data.Entity.EntityState.Modified; } else // add { db.Students.Add(student); //var savedIndex = MvcApplication.Students.FindIndex(x => x.Id == student.Id); //MvcApplication.Students.RemoveAt(savedIndex); } db.SaveChanges(); return(string.Format("{0} {1}", student.FirstName, student.LastName)); }
public string Post(Course course) { Course courseToUpdate = null; if (course.Id > 0) { courseToUpdate = db.Courses.Include("Students").First(i => i.Id == course.Id); } else { courseToUpdate = new Course(); } courseToUpdate.Active = course.Active; courseToUpdate.Credits = course.Credits; courseToUpdate.Name = course.Name; courseToUpdate.Term = course.Term; courseToUpdate.Year = course.Year; foreach (var student in db.Students) { if (!course.Students.Any(item => item.Id == student.Id)) { courseToUpdate.Students.Remove(student); } else { courseToUpdate.Students.Add((student)); } } if (course.Id > 0) { db.Entry(courseToUpdate).State = EntityState.Modified; } else { db.Courses.Add(courseToUpdate); } db.SaveChanges(); return(course.Name); }
public async Task <IHttpActionResult> PutVehicle(int id, Vehicle vehicle) { if (vehicle.StandardVehicle == true) { List <Vehicle> vehicles = db.Vehicles.Where(x => x.StandardVehicle == true).ToList(); vehicles.Select(x => { x.StandardVehicle = false; return(x); }).ToList(); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != vehicle.Id) { return(BadRequest()); } db.Entry(vehicle).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!VehicleExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }