public async Task <ActionResult> Cars_Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")] IEnumerable <CarViewModel> cars) { foreach (var car in cars) { if (car != null && ModelState.IsValid) { var newCar = new Car { Name = car.Name }; if (car.CategoryId.HasValue) { newCar.CategoryId = car.CategoryId.Value; } else { var category = new Category { Name = car.CategoryName }; db.Entry(category).State = EntityState.Added; newCar.Category = category; } db.Cars.Add(newCar); await db.SaveChangesAsync(); car.Id = newCar.Id; } } await db.SaveChangesAsync(); return(Json(cars.ToDataSourceResult(request, ModelState))); }
public async Task <IHttpActionResult> PutCar(int id, Car car) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != car.Id) { return(BadRequest()); } _db.Entry(car).State = EntityState.Modified; try { await _db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CarExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutCarsForSale(int id, CarsForSale carsForSale) { if (id != carsForSale.Id) { return(BadRequest()); } _context.Entry(carsForSale).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CarsForSaleExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult Edit([Bind(Include = "ID,Year,Make,Model,Color,Engine")] Cars cars) { if (ModelState.IsValid) { db.Entry(cars).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(cars)); }
public ActionResult Edit([Bind(Include = "ID,CarName,IsChecked")] Cars cars) { if (ModelState.IsValid) { db.Entry(cars).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(cars)); }
public async Task <ActionResult <Car> > PutCar(int id, Car car) { if (id != car.CarId) { return(BadRequest()); } _context.Entry(car).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult> UpdateCar(Cars updatedCar) { if (!ModelState.IsValid) { return(BadRequest()); } else { _context.Entry(updatedCar).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); } }
public async Task <ActionResult> PutCar(int id, Car car) { if (id != car.Id) { return(BadRequest()); } _context.Entry(car).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); //response 204 (No content) - requires the client to send an entirely updated entity and not just the chanes //to support partial updates, we would use HTTP Patch }
public ActionResult ModifyCar(int id, [FromBody] Car Car) { var target = dbContext.Car.SingleOrDefault(car => car.ID == id);; if (target != null && ModelState.IsValid) { dbContext.Entry(target).CurrentValues.SetValues(Car); dbContext.SaveChanges(); return(Ok()); } else { return(BadRequest()); } }
public IHttpActionResult UpdateCar(int id, [FromBody] Car car) { if (ModelState.IsValid) { var carExists = dbContext.Cars.Count(c => c.Id == id) > 0; if (carExists) { dbContext.Entry(car).State = EntityState.Modified; dbContext.SaveChanges(); return(Ok()); } else { return(NotFound()); } } else { return(BadRequest()); } }