public ActionResult City_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]  IEnumerable <DC_Location_City> listEx)
 {
     if (asset.Update)
     {
         try
         {
             if (listEx != null && ModelState.IsValid)
             {
                 foreach (var regis in listEx)
                 {
                     if (String.IsNullOrEmpty(regis.CityName))
                     {
                         ModelState.AddModelError("", "Please input City Name ");
                         return(Json(listEx.ToDataSourceResult(request, ModelState)));
                     }
                     //if (String.IsNullOrEmpty(regis.RegionID))
                     //{
                     //    ModelState.AddModelError("", "Please input Region Name");
                     //    return Json(listEx.ToDataSourceResult(request, ModelState));
                     //}
                     var write = new DC_Location_City();
                     var check = DC_Location_City.GetDC_Location_Citys("1=1", "").Where(s => s.CityName.Trim().ToLower() == regis.CityName.Trim().ToLower() && s.RegionID == regis.RegionID && s.Active == regis.Active);
                     if (check.Count() > 0)
                     {
                         ModelState.AddModelError("", "Region Name is exists.");
                         return(Json(listEx.ToDataSourceResult(request, ModelState)));
                     }
                     write.CityID             = regis.CityID;
                     write.CityName           = regis.CityName.Trim();
                     write.Active             = regis.Active;
                     write.RowLastUpdatedTime = DateTime.Now;
                     write.RowLastUpdatedUser = currentUser.UserName;
                     write.RegionID           = regis.RegionID != null ? regis.RegionID : "";
                     write.Update();
                 }
             }
             else
             {
                 ModelState.AddModelError("error", "");
                 return(Json(new { success = false }));
             }
         }
         catch (Exception e)
         {
             ModelState.AddModelError("error", e.Message);
             return(Json(listEx.ToDataSourceResult(request, ModelState)));
         }
     }
     else
     {
         ModelState.AddModelError("", "You don't have permission to update record");
         return(Json(listEx.ToDataSourceResult(request, ModelState)));
     }
     return(Json(listEx.ToDataSourceResult(request, ModelState)));
 }
        public ActionResult ImportFromExcel()
        {
            try
            {
                if (Request.Files["FileUpload"] != null && Request.Files["FileUpload"].ContentLength > 0)
                {
                    string fileExtension =
                        System.IO.Path.GetExtension(Request.Files["FileUpload"].FileName);

                    if (fileExtension == ".xlsx")
                    {
                        string fileLocation      = string.Format("{0}/{1}", Server.MapPath("~/Excel"), "[" + currentUser.UserName + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "]" + Request.Files["FileUpload"].FileName);
                        string errorFileLocation = string.Format("{0}/{1}", Server.MapPath("~/Excel"), "[" + currentUser.UserName + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-Error]" + Request.Files["FileUpload"].FileName);

                        if (System.IO.File.Exists(fileLocation))
                        {
                            System.IO.File.Delete(fileLocation);
                        }

                        Request.Files["FileUpload"].SaveAs(fileLocation);
                        //Request.Files["fileUpload"].SaveAs(errorFileLocation);

                        var rownumber = 2;
                        var total     = 0;

                        FileInfo fileInfo = new FileInfo(fileLocation);
                        var      excelPkg = new ExcelPackage(fileInfo);

                        FileInfo template = new FileInfo(Server.MapPath(@"~\ExportExcelFile\DC_Location_City.xlsx"));
                        template.CopyTo(errorFileLocation);
                        FileInfo _fileInfo = new FileInfo(errorFileLocation);
                        var      _excelPkg = new ExcelPackage(_fileInfo);

                        ExcelWorksheet oSheet = excelPkg.Workbook.Worksheets["DC_Location_City"];
                        ExcelWorksheet eSheet = _excelPkg.Workbook.Worksheets["DC_Location_City"];

                        //remove row
                        int totalRows = oSheet.Dimension.End.Row;
                        for (int i = 2; i <= totalRows; i++)
                        {
                            string cityID     = oSheet.Cells[i, 1].Value != null ? oSheet.Cells[i, 1].Value.ToString() : "";
                            string cityName   = oSheet.Cells[i, 2].Value != null ? oSheet.Cells[i, 2].Value.ToString() : "";
                            string regionName = oSheet.Cells[i, 3].Value != null ? oSheet.Cells[i, 3].Value.ToString() : "";
                            //string countryName = oSheet.Cells[i, 3].Value != null ? oSheet.Cells[i, 3].Value.ToString() : "";
                            string active     = oSheet.Cells[i, 6].Value != null ? oSheet.Cells[i, 6].Value.ToString() : "TRUE";
                            string id_country = !String.IsNullOrEmpty(regionName) ? regionName.Substring(0, regionName.LastIndexOf("-")).Trim() : "";

                            try
                            {
                                var write        = new DC_Location_City();
                                var checkRegion  = DC_Location_City.GetDC_Location_Citys("[CityName] = N'" + cityName + "' AND [RegionID] = '" + id_country + "'", "").FirstOrDefault();
                                var checkCountry = DC_Location_City.GetDC_Location_Citys("[RegionID] = '" + id_country + "'", "").FirstOrDefault();
                                if (string.IsNullOrEmpty(regionName.ToString()) || string.IsNullOrEmpty(cityName.ToString()))
                                {
                                    eSheet.Cells[rownumber, 2].Value  = cityName;
                                    eSheet.Cells[rownumber, 3].Value  = regionName;
                                    eSheet.Cells[rownumber, 6].Value  = active;
                                    eSheet.Cells[rownumber, 11].Value = "regionName, countryName required";
                                    rownumber++;
                                }
                                else if (checkRegion != null)
                                {
                                    write.CityID             = cityID;
                                    write.CityName           = cityName;
                                    write.Active             = Convert.ToBoolean(active);
                                    write.RowLastUpdatedTime = DateTime.Now;
                                    write.RowLastUpdatedUser = currentUser.UserName;
                                    write.RegionID           = id_country;
                                    write.Update();
                                    total++;
                                }
                                //else if (checkCountry == null)
                                //{
                                //    eSheet.Cells[rownumber, 2].Value = cityName;
                                //    eSheet.Cells[rownumber, 3].Value = regionName;
                                //    eSheet.Cells[rownumber, 6].Value = active;
                                //    eSheet.Cells[rownumber, 11].Value = "countryName not exist in system";
                                //    rownumber++;
                                //}
                                else
                                {
                                    string id      = "";
                                    var    checkID = DC_Location_City.GetDC_Location_Citys("1=1", "").OrderByDescending(m => m.RowID).FirstOrDefault();
                                    if (checkID != null)
                                    {
                                        var nextNo = Int32.Parse(checkID.CityID.Substring(1, checkID.CityID.Length - 1)) + 1;
                                        id = "A" + String.Format("{0:0000}", nextNo);
                                    }
                                    else
                                    {
                                        id = "A0001";
                                    }

                                    write.CityID         = id;
                                    write.CityName       = cityName;
                                    write.Active         = bool.Parse(active);
                                    write.RowCreatedTime = DateTime.Now;
                                    write.RowCreatedUser = currentUser.UserName;
                                    write.RegionID       = id_country;
                                    write.Save();
                                    total++;
                                }
                            }
                            catch (Exception e)
                            {
                                eSheet.Cells[rownumber, 2].Value  = cityName;
                                eSheet.Cells[rownumber, 3].Value  = regionName;
                                eSheet.Cells[rownumber, 5].Value  = active;
                                eSheet.Cells[rownumber, 10].Value = e.Message;
                                rownumber++;
                                continue;
                            }
                        }
                        _excelPkg.Save();

                        return(Json(new { success = true, total = total, totalError = rownumber - 2, link = errorFileLocation }));
                    }
                    else
                    {
                        return(Json(new { success = false, error = "File extension is not valid. *.xlsx please." }));
                    }
                }
                else
                {
                    return(Json(new { success = false, error = "File upload null" }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new { success = false, error = ex.Message }));
            }
        }