4

I have did the excel upload in dotnet core .I had to use tempdata to retrieve the details of the excel in list.Instead in my below code i had used Static object to retrieve the list.My code works as like this ,when i click on upload button it will display the details in the excel sheet.and when click on save it will save it to database and i need to edit in grid view using ajax call also .Help me out

My Action in controller is

public async Task<IActionResult> ImportEmployeeDetails(IFormFile excelfile)
{
        try
        {
            EmployeesViewModelList employeesListObject = new EmployeesViewModelList();
            List<EmployeeModel> employeesViewModelList = new List<EmployeeModel>();
            if (excelfile == null || excelfile.Length == 0)
            {
                return View(employeesListObject);
            }
            var supportedTypes = new[] { ".xls", ".xlsx" };
            var ext = Path.GetExtension(excelfile.FileName);
            if (!supportedTypes.Contains(ext))
            {
                return View(employeesListObject);
            }
            var path = Path.Combine(
                       Directory.GetCurrentDirectory(), "wwwroot",
                       "EmployeeDetails.xlsx");

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

            using (var stream = new FileStream(path, FileMode.Create))
            {
                await excelfile.CopyToAsync(stream);
            }
            FileInfo file = new FileInfo(path);
            using (ExcelPackage package = new ExcelPackage(file))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                int rowCount = worksheet.Dimension.Rows;
                int ColCount = worksheet.Dimension.Columns;

                for (int i = 2; i <= rowCount; i++)
                {
                    EmployeeModel emp = new EmployeeModel();
                    emp.EmployeeId = Convert.ToInt32(worksheet.Cells[i, 1].Value.ToString());

                    emp.EmpFirstName = worksheet.Cells[i, 2].Value.ToString();
                    employeesViewModelList.Add(emp);

                }

                employeesListObject.EmpModelList = employeesViewModelList;
                return View(employeesListObject);

            }
        }
        catch(Exception ex)
        {
            TempData["Message"] = "Opps! Something Went wrong!";
            return RedirectToAction("ExcelPackage");
        }
}
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
anjalin sneha
  • 61
  • 1
  • 7
  • Can provide more details on what you are looking to do with TempData – MrLu May 01 '19 at 17:21
  • I need to read my excel detail in the same page once i clicked on upload button .I have to store the list using tempdata and retrieve it – anjalin sneha May 02 '19 at 04:13

1 Answers1

4

Try this, using your own list.

List<string> SomeList = new List<string>();

TempData["MyList"] = SomeList;

//then to get data just do 

SomeList = TempData["MyList"] as List<string>; //This converts back to List<T>

Once you add the list to the TempData, you can retrive it from any Action or View in the same controller

MrLu
  • 376
  • 2
  • 9