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)));
        }
예제 #2
0
        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));
        }
예제 #3
0
        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());
        }
예제 #4
0
 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));
 }
예제 #5
0
 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));
 }
예제 #6
0
        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());
        }
예제 #7
0
        public async Task <ActionResult> UpdateCar(Cars updatedCar)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            else
            {
                _context.Entry(updatedCar).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(NoContent());
            }
        }
예제 #8
0
        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
        }
예제 #9
0
        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());
            }
        }
예제 #10
0
        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());
            }
        }