public 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 { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CarExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "Make,Model,Year,Mileage,Used,ID")] Car car) { if (ModelState.IsValid) { db.Cars.Add(car); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(car)); }
public ActionResult Index(string userName, string password, string sex) { UserInfo user = new UserInfo() { UserName = userName, UserPwd = password, UserSex = sex, }; FEContext.UserInfo.Add(user); FEContext.SaveChanges(); return(RedirectToRoute(new { controller = "Login", action = "Index" })); }
private static void CreateUser(CarEntities context, string email, string role, string firstName, string lastName) { if (!context.Users.Any(s => s.Email == email)) { var user = new Models.User { Email = email, FirstName = firstName, LastName = lastName, Password = "******", Role = role }; Salesperson sales = new Salesperson(); sales.User = user; context.Users.Add(user); context.Salespersons.Add(sales); context.SaveChanges(); } }
public VehicleViewModel SaveVehicle(VehicleViewModel model) { Vehicle vehicle; if (model.Id > 0)//Add { vehicle = db.Vehicles.Where(s => s.Id == model.Id).FirstOrDefault(); if (vehicle == null) { throw new Exception($"Vehicle with Id: {model.Id} not found."); } } //edit else { vehicle = new Vehicle(); db.Vehicles.Add(vehicle); } if (vehicle.Stocks == null || vehicle.Stocks.Count == 0) { vehicle.Stocks.Add(new Stock { FloorPrice = model.SalesPrice.HasValue ? model.SalesPrice.Value : 0, Feature = BitConverter.GetBytes(model.IsFeaturedVehicle), Available = BitConverter.GetBytes(model.IsAvailable) }); } else { var stock = vehicle.Stocks.FirstOrDefault(); if (stock != null) { stock.Available = BitConverter.GetBytes(model.IsAvailable); stock.Feature = BitConverter.GetBytes(model.IsFeaturedVehicle); stock.FloorPrice = model.SalesPrice.HasValue ? model.SalesPrice.Value : 0; } } MapVehicleViewModelToDbModel(model, vehicle); try { db.SaveChanges(); } catch (DbEntityValidationException e) { var errorMessage = string.Empty; foreach (var eve in e.EntityValidationErrors) { errorMessage += string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { errorMessage += string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } model.Id = vehicle.Id; return(model); }