Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,FishId,FamilyId,FishNameKK,FishNameRU,FishNameLA,FamilyNameKK,FamilyNameRU,FamilyNameLA")] FishKind fishKind)
        {
            if (id != fishKind.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(fishKind);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FishKindExists(fishKind.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(fishKind));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("Id,FishId,FamilyId,FishNameKK,FishNameRU,FishNameLA,FamilyNameKK,FamilyNameRU,FamilyNameLA")] FishKind fishKind)
        {
            if (ModelState.IsValid)
            {
                _context.Add(fishKind);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(fishKind));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Upload(bool FirstRowHeader, IFormFile File)
        {
            try
            {
                string sContentRootPath = _hostingEnvironment.WebRootPath;
                sContentRootPath = Path.Combine(sContentRootPath, "Uploads");
                DirectoryInfo di = new DirectoryInfo(sContentRootPath);
                foreach (FileInfo filed in di.GetFiles())
                {
                    try
                    {
                        filed.Delete();
                    }
                    catch
                    {
                    }
                }
                string path_filename = Path.Combine(sContentRootPath, Path.GetFileName(File.FileName));
                using (var stream = new FileStream(Path.GetFullPath(path_filename), FileMode.Create))
                {
                    await File.CopyToAsync(stream);
                }
                FileInfo fileinfo = new FileInfo(Path.Combine(sContentRootPath, Path.GetFileName(path_filename)));
                using (ExcelPackage package = new ExcelPackage(fileinfo))
                {
                    int start_row = 1;
                    if (FirstRowHeader)
                    {
                        start_row++;
                    }
                    List <FishKind> fishKinds = new List <FishKind>();
                    for (int i = start_row; ; i++)
                    {
                        if (package.Workbook.Worksheets.FirstOrDefault().Cells[i, 1].Value == null)
                        {
                            break;
                        }
                        FishKind fishKind = new FishKind();

                        try
                        {
                            fishKind.FishId       = Convert.ToInt32(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 1].Value);
                            fishKind.FamilyId     = Convert.ToInt32(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 2].Value);
                            fishKind.FishNameKK   = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 3].Value.ToString();
                            fishKind.FishNameRU   = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 4].Value.ToString();
                            fishKind.FishNameLA   = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 5].Value.ToString();
                            fishKind.FamilyNameKK = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 6].Value.ToString();
                            fishKind.FamilyNameRU = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 7].Value.ToString();
                            fishKind.FamilyNameLA = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 8].Value.ToString();
                        }
                        catch (Exception e)
                        {
                            ViewBag.Error = $"{_sharedLocalizer["Row"]} {i.ToString()}: " + e.Message + (e.InnerException == null ? "" : ": " + e.InnerException.Message);
                            break;
                        }

                        fishKinds.Add(fishKind);
                        _context.Add(fishKinds.LastOrDefault());
                    }
                    if (string.IsNullOrEmpty(ViewBag.Error))
                    {
                        _context.SaveChanges();
                        ViewBag.Report = $"{_sharedLocalizer["UploadedCount"]}: {fishKinds.Count()}";
                    }
                }
                foreach (FileInfo filed in di.GetFiles())
                {
                    try
                    {
                        filed.Delete();
                    }
                    catch
                    {
                    }
                }
            }
            catch (Exception e)
            {
                if (File != null)
                {
                    ViewBag.Error = e.Message + (e.InnerException == null ? "" : ": " + e.InnerException.Message);
                }
            }
            return(View());
        }