public ActionResult SaveLoadings([FromBody] VehicleLoading data) { try { var context = new RRRoadwaysDBContext(); context.Add(data); context.SaveChanges(); ViewBag.result = "Record Saved Successfully!"; } catch (Exception e) { var error = e; ViewBag.error = e.Message; } ModelState.Clear(); return(RedirectToAction("Index")); }
public IActionResult Add(VehicleLoading data) { try { var context = new RRRoadwaysDBContext(); data.EntryDate = DateTime.Now.Date; context.Add(data); context.SaveChanges(); ViewBag.vehicleId = new SelectList(context.Vehicle.Where(x => x.IsDeleted == false).ToList(), "Id", "VehicleNumber"); ViewBag.voucherId = new SelectList(context.Voucher.Where(x => x.IsDeleted == false).ToList(), "Id", "VoucherNumber"); ViewBag.result = "Record Saved Successfully!"; } catch (Exception e) { var error = e; ViewBag.error = e.Message; } ModelState.Clear(); return(RedirectToAction("Index")); }
public IActionResult Edit(VehicleLoading data) { try { var context = new RRRoadwaysDBContext(); var dbEntry = context.Entry(data); dbEntry.Property("LoadingId").IsModified = true; dbEntry.Property("VehicleId").IsModified = true; dbEntry.Property("LoadingDate").IsModified = true; context.SaveChanges(); ViewBag.vehicleId = new SelectList(context.Vehicle.Where(x => x.IsDeleted == false).ToList(), "Id", "VehicleNumber"); ViewBag.voucherId = new SelectList(context.Voucher.Where(x => x.IsDeleted == false).ToList(), "Id", "VoucherNumber"); ViewBag.result = "Record Updated Successfully!"; } catch (Exception e) { var error = e; ViewBag.error = e.Message; } return(RedirectToAction("Index")); }
public IActionResult Edit([FromBody] Voucher data) { try { var context = new RRRoadwaysDBContext(); var dbEntry = context.Entry(data); context.Entry(data).State = EntityState.Modified; context.SaveChanges(); List <int> previousExpanseIds = context.VoucherOthersExpenses .Where(ep => ep.VoucherId == data.Id) .Select(ep => ep.Id) .ToList(); List <Voucher> Vouchers = context.Voucher.Where(v => v.Id == data.Id).ToList(); List <VehicleLoading> VehicleLoadings = context.VehicleLoading.Where(v => v.VoucherId == data.Id).ToList(); List <VehicleLoadingDetail> VehicleLoadingDetails = context.VehicleLoadingDetail.Where(x => x.IsDeleted == false).ToList(); List <int> previousLoadingIds = (from vl in VehicleLoadings join vld in VehicleLoadingDetails on vl.Id equals vld.VloadingId select new { vld.Id } ).Select(o => o.Id).ToList(); List <int> previousDieselIds = context.VoucherDieselDetails .Where(vl => vl.VoucherId == data.Id) .Select(vl => vl.Id) .ToList(); List <int> currentLoadingDetailIds = new List <int>(); //foreach (VehicleLoading itemx in data.VehicleLoading.ElementAt(0)) //{ VehicleLoading itemx = data.VehicleLoading.ElementAt(0); foreach (VehicleLoadingDetail item in itemx.VehicleLoadingDetail) { currentLoadingDetailIds.Add(item.Id); } //} List <int> currentExpanseIds = data.VoucherOthersExpenses.Select(o => o.Id).ToList(); List <int> currentDieselIds = data.VoucherDieselDetails.Select(o => o.Id).ToList(); List <int> deletedExpanseIds = previousExpanseIds.Except(currentExpanseIds).ToList(); List <int> deletedDieselIds = previousDieselIds.Except(currentDieselIds).ToList(); List <int> deletedLoadingDetailIds = previousLoadingIds.Except(currentLoadingDetailIds).ToList(); foreach (var ExpanseDetail in data.VoucherOthersExpenses) { if (ExpanseDetail.Id == 0) { context.Entry(ExpanseDetail).State = EntityState.Added; ExpanseDetail.VoucherId = data.Id; } else { context.Entry(ExpanseDetail).State = EntityState.Modified; ExpanseDetail.VoucherId = data.Id; } } foreach (var deletedExpanseId in deletedExpanseIds) { VoucherOthersExpenses deletedExpanse = context.VoucherOthersExpenses.Where(od => od.VoucherId == data.Id && od.Id == deletedExpanseId).Single(); context.Entry(deletedExpanse).State = EntityState.Deleted; } context.SaveChanges(); foreach (var DieselDetail in data.VoucherDieselDetails) { if (DieselDetail.Id == 0) { context.Entry(DieselDetail).State = EntityState.Added; DieselDetail.VoucherId = data.Id; } else { context.Entry(DieselDetail).State = EntityState.Modified; DieselDetail.VoucherId = data.Id; } } foreach (var deleteDieselId in deletedDieselIds) { VoucherDieselDetails deletedDiesel = context.VoucherDieselDetails.Where(od => od.VoucherId == data.Id && od.Id == deleteDieselId).Single(); context.Entry(deletedDiesel).State = EntityState.Deleted; } context.SaveChanges(); VehicleLoading Loading = data.VehicleLoading.ElementAt(0); //foreach (var Loading in data.VehicleLoading) //{ if (Loading.Id == 0) { Loading.VoucherId = data.Id; context.Entry(Loading).State = EntityState.Added; } else { Loading.VoucherId = data.Id; context.Entry(Loading).State = EntityState.Detached; } foreach (var LoadingDetail in Loading.VehicleLoadingDetail) { if (LoadingDetail.Id == 0) { LoadingDetail.VloadingId = Loading.Id; context.Entry(LoadingDetail).State = EntityState.Added; } else { LoadingDetail.VloadingId = Loading.Id; context.Entry(LoadingDetail).State = EntityState.Detached; } } //context.Entry(Loading).State = EntityState.Modified; //Loading.VoucherId = data.Id; //} foreach (var deleteLoadingDetailId in deletedLoadingDetailIds) { VehicleLoadingDetail deletedVehicleLoadingDetail = context.VehicleLoadingDetail.Where(od => od.Id == deleteLoadingDetailId).Single(); context.Entry(deletedVehicleLoadingDetail).State = EntityState.Deleted; } context.SaveChanges(); ViewBag.vehicleId = new SelectList(context.Vehicle.Where(x => x.IsDeleted == false).ToList(), "Id", "VehicleNumber"); ViewBag.result = "**Record Updated Successfully!"; } catch (Exception e) { var error = e; ViewBag.error = e.Message; } return(Json(ViewBag.result, new Newtonsoft.Json.JsonSerializerSettings())); //return RedirectToAction("Index"); }