Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("SourceName,SourceCode,NameFile,FormatFile,HasFile,Id")] SourceSanction sourceSanction)
        {
            if (id != sourceSanction.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(sourceSanction);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SourceSanctionExists(sourceSanction.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(sourceSanction));
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, SourceSanction model, IFormFile file)
        {
            if (model == null)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                try
                {
                    if (!Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
                    {
                        ViewBag.ErrorMessage = "Not Support file extension";
                        return(View("Index"));
                    }
                    var            sanctions      = new List <Sanction>();
                    SourceSanction sourceSanction = new SourceSanction();
                    sourceSanction.Id         = id;
                    sourceSanction.FormatFile = model.FormatFile;
                    sourceSanction.HasFile    = model.HasFile;
                    sourceSanction.NameFile   = model.NameFile;
                    sourceSanction.SourceCode = model.SourceCode;
                    sourceSanction.SourceName = model.SourceName;
                    using (var stream = new MemoryStream())
                    {
                        string importFolder = Path.Combine(_hostingEnvironment.WebRootPath, "ImportFiles");
                        string filePath     = Path.Combine(importFolder, file.FileName);
                        await file.CopyToAsync(stream);

                        using (var package = new ExcelPackage(stream))
                        {
                            ExcelWorksheet worksheet     = package.Workbook.Worksheets[0];
                            var            rowCount      = worksheet.Dimension.Rows;
                            var            counterRecord = 0;
                            for (int row = 2; row <= rowCount; row++)
                            {
                                if (!CheckHasRecord(id, worksheet.Cells[row, 1].Value.ToString().Trim()))
                                {
                                    model.Sanctions.Add(new Sanction
                                    {
                                        RefrenceId            = worksheet.Cells[row, 1].Value == null ? "" : worksheet.Cells[row, 1].Value.ToString().Trim(),
                                        LegalName             = worksheet.Cells[row, 2].Value == null ? "" : worksheet.Cells[row, 2].Value.ToString().Trim(),
                                        EntityType            = worksheet.Cells[row, 3].Value == null ? "" : worksheet.Cells[row, 3].Value.ToString().Trim(),
                                        NameType              = worksheet.Cells[row, 4].Value == null ? "" : worksheet.Cells[row, 4].Value.ToString().Trim(),
                                        DateofBirth           = worksheet.Cells[row, 5].Value == null ? "" :  worksheet.Cells[row, 5].Value.ToString().Trim(),
                                        PlaceofBirth          = worksheet.Cells[row, 6].Value == null ? "" :  worksheet.Cells[row, 6].Value.ToString().Trim(),
                                        Citizenship           = worksheet.Cells[row, 7].Value == null ? "" : worksheet.Cells[row, 7].Value.ToString().Trim(),
                                        Address               = worksheet.Cells[row, 8].Value == null ? "" : worksheet.Cells[row, 8].Value.ToString().Trim(),
                                        AdditionalInformation = worksheet.Cells[row, 9].Value == null ? "" : worksheet.Cells[row, 9].Value.ToString().Trim(),
                                        ListingInformation    = worksheet.Cells[row, 10].Value == null ? "" : worksheet.Cells[row, 10].Value.ToString().Trim(),
                                        Committees            = worksheet.Cells[row, 11].Value == null ? "" : worksheet.Cells[row, 11].Value.ToString().Trim(),
                                        ControlDate           = worksheet.Cells[row, 12].Value == null ? "" : worksheet.Cells[row, 12].Value.ToString().Trim(),
                                        InsertDate            = DateTime.UtcNow.Date.ToString(),
                                        IsActive              = Convert.ToByte(1),
                                        SactionUID            = Guid.NewGuid()
                                    });
                                }
                                if (counterRecord == 10)
                                {
                                    sourceSanction.Sanctions = model.Sanctions;
                                    _context.Update(sourceSanction);
                                    await _context.SaveChangesAsync();

                                    counterRecord = 0;
                                }
                                else
                                {
                                    counterRecord++;
                                }
                            }
                        }
                    }
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    if (!SourceSanctionExists(model.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }