gpt4 book ai didi

asp.net - 如何使用Tempdata显示列表

转载 作者:行者123 更新时间:2023-12-04 14:20:02 24 4
gpt4 key购买 nike

我已经在 dotnet core 中上传了 excel。我不得不使用 tempdata 来检索列表中 excel 的详细信息。相反,在我下面的代码中,我使用了静态对象来检索列表。我的代码是这样工作的,当我点击时在上传按钮上,它会在 Excel 表格中显示详细信息。当点击保存时,它会将其保存到数据库中,我还需要使用 ajax 调用在网格 View 中进行编辑。帮我解决

我在 Controller 中的操作是

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");
}
}

最佳答案

试试这个,使用你自己的列表。

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>

将列表添加到 TempData 后,您可以从同一 Controller 中的任何操作或 View 中检索它

关于asp.net - 如何使用Tempdata显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55938021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com