public ActionResult Create([Bind(Include = "RegNr, Owner, Color, VehicleType, NumberOfWheels, InTime, OutTime")] CreateViewModel newVehicle) { if (ModelState.IsValid && Request.IsAjaxRequest()) { /// default vehicle prices from the model Prices.cs /// //newVehicle.TotalMoneyAmount = price; Vehicle v = new Vehicle(); foreach (PropertyInfo p in typeof(CreateViewModel).GetProperties()) { PropertyInfo modelproperty = typeof(CreateViewModel).GetProperty(p.Name); PropertyInfo vehicleproperty = typeof(Vehicle).GetProperty(p.Name); vehicleproperty.SetValue(v, modelproperty.GetValue(newVehicle)); } var dv = Garage.AddVehicle(v); if (dv.exist == false) { return(Json(new { type = true, message = string.Format("Vehicle {0} was created", v.RegNr), function = "Checkin", id = dv.Id })); } else { return(Json(new { type = true, message = string.Format("Vehicle {0} Already exist", v.RegNr), function = "Checkin", id = dv.Id })); } } return(RedirectToAction("Index")); }
public IHttpActionResult Post([FromBody] VehicleViewModel vm) { if (ModelState.IsValid) { repo.AddVehicle(vm); return(Ok("Vehicle added successfully! " + vm)); } return(BadRequest("ModelState is not valid!")); }
public ActionResult Create([Bind(Include = "Id,Color,RegNr,TypeId,OwnerId")] Vehicle vehicle) { if (ModelState.IsValid) { db.AddVehicle(vehicle); return(RedirectToAction("Index")); } ViewBag.OwnerId = new SelectList(db.GetOwners(), "Id", "Name", vehicle.OwnerId); ViewBag.TypeId = new SelectList(db.GetVehicleTypes(), "Id", "Name", vehicle.TypeId); return(View(vehicle)); }