public ActionResult Update() { bool status = false; string message = ""; if (Request.Form.Count > 0) { var models = JsonConvert.DeserializeObject <List <StateProvinceWBModel> >(Request.Form[0]); if (models != null) { var model = models[0]; var stateProvince = _stateProvinceService.GetStateProvinceById(model.Id); if (stateProvince != null && stateProvince != null) { stateProvince.Name = model.Name; stateProvince.Abbreviation = model.Abbreviation; _stateProvinceService.UpdateStateProvince(stateProvince); var stateProvinceWBs = _stateProvinceWBService.GetStateProvinceWBsByStateProvinceId(stateProvince.Id); var postalCodes = new List <String>(); if (!String.IsNullOrEmpty(model.PostalCode)) { postalCodes = model.PostalCode.Split(',').ToList(); } if (stateProvinceWBs != null) { var nonList = stateProvinceWBs.Where(x => !postalCodes.Contains(x.PostalCode)).ToList(); if (nonList != null) { _stateProvinceWBService.Delete(nonList); } foreach (var postalCode in postalCodes) { if (_stateProvinceWBService.GetByPostalCodeAndProvinceID(postalCode, stateProvince.Id) == null) { var stateProvinceWB = new StateProvincePostalCode { Id = 0, PostalCode = postalCode, StateProvinceID = stateProvince.Id }; _stateProvinceWBService.Insert(stateProvinceWB); } } } } model = _stateProvinceWBService.GetStateProvinceWBModelByStateProvinceId(model.Id); return(Json(model)); } } return(Json(new { Status = status, Message = message })); }
public void ImportProvincesFromXlsx(Stream stream, int countryId) { using (var xlPackage = new ExcelPackage(stream)) { // get the first worksheet in the workbook var worksheet = xlPackage.Workbook.Worksheets.FirstOrDefault(); if (worksheet == null) { throw new NopException("No worksheet found"); } //the columns var properties = GetPropertiesByExcelCells <StateProvince>(worksheet); var manager = new PropertyManager <StateProvince>(properties); var iRow = 2; var setSeName = properties.Any(p => p.PropertyName == "SeName"); while (true) { var allColumnsAreEmpty = manager.GetProperties .Select(property => worksheet.Cells[iRow, property.PropertyOrderPosition]) .All(cell => cell == null || cell.Value == null || String.IsNullOrEmpty(cell.Value.ToString())); if (allColumnsAreEmpty) { break; } manager.ReadFromXlsx(worksheet, iRow); //var seName = string.Empty; string name = ""; string postalCode = ""; string abbreviation = ""; foreach (var property in manager.GetProperties) { switch (property.PropertyName) { case "Name": name = property.StringValue; break; case "PostalCode": postalCode = property.StringValue; break; case "Abbreviation": abbreviation = property.StringValue; break; } } var stateProvince = _stateProvinceService.GetStateProvinceByAbbreviation(abbreviation) ?? new StateProvince { Published = true, CountryId = countryId, Name = name, Abbreviation = abbreviation, Id = 0 }; var isNew = stateProvince.Id == 0; if (isNew) { _stateProvinceService.InsertStateProvince(stateProvince); } else { _stateProvinceService.UpdateStateProvince(stateProvince); } if (stateProvince.Id > 0) { var postalCodes = postalCode.Split(',').ToList(); if (postalCodes != null) { foreach (var postal in postalCodes) { var stateProvinceWB = _stateProvinceWBService.GetByPostalCodeAndProvinceID(postal, stateProvince.Id) ?? new StateProvincePostalCode { Id = 0, StateProvinceID = stateProvince.Id, PostalCode = postal }; if (stateProvinceWB.Id == 0) { _stateProvinceWBService.Insert(stateProvinceWB); } } } } iRow++; } } }