public ActionResult Upload(UploadedExcelSheetViewModel viewmodel) { if (ModelState.IsValid) // validate file exist { if (viewmodel.ExcelFile != null && viewmodel.ExcelFile.ContentLength > 0) { var fileName = Path.GetFileName(viewmodel.ExcelFile.FileName); var path = Path.Combine(Server.MapPath("~/Uploads/PaySources/"), DateTime.Now.GetTimeStamp() + "_" + fileName); List <PaySource> addedPaySources = new List <PaySource>(); viewmodel.ExcelFile.SaveAs(path); // save a copy of the uploaded file. // convert the uploaded file into datatable, then add/update db entities. var dtServers = ImportUtils.ImportXlsxToDataTable(viewmodel.ExcelFile.InputStream, true); int numOfPaySourcesUpdated = 0; foreach (var row in dtServers.AsEnumerable().ToList()) { var entityViewModel = new PaySourceAddViewModel() { VendorId = int.Parse(row["PaySourceId"].ToString()), // some columns does not have ',' separater. Description = row["Description"].ToString(), //Active = row["active"].ToString() == "Y" ? true : false, }; //check if paysource does not exist if (!string.IsNullOrWhiteSpace(row["PaySourceId"].ToString())) { var existedEntity = _paySourceService.GetByVendorId(entityViewModel.VendorId); if (existedEntity == null) { var entity = Mapper.Map <PaySourceAddViewModel, PaySource>(entityViewModel); addedPaySources.Add(entity); } else { Mapper.Map(entityViewModel, existedEntity); _paySourceService.UpdatePaySource(existedEntity); numOfPaySourcesUpdated++; } } } if (addedPaySources.Any()) { _paySourceService.AddPaySources(addedPaySources); } Success($"<strong>{addedPaySources.Count}</strong> PaySources have been successfully added. <br\\>" + $"<strong>{numOfPaySourcesUpdated}</strong> PaySources have been successfully updated."); } return(RedirectToAction("Index")); } return(View(viewmodel)); }
public ActionResult Create(PaySourceAddViewModel viewmodel) { if (ModelState.IsValid) { // check if vendor id already exists. if (_paySourceService.GetByVendorId(viewmodel.VendorId) == null) { var entity = Mapper.Map <PaySourceAddViewModel, PaySource>(viewmodel); _paySourceService.AddPaySource(entity); Success($"<strong>{entity.Description}</strong> was successfully added."); return(RedirectToAction("Index")); } else { Danger($"A Paysource with same Id <strong>{viewmodel.VendorId}</strong> already exists."); } } return(View(viewmodel)); }
public ActionResult Create() { var viewmodel = new PaySourceAddViewModel(); return(View(viewmodel)); }