【发布时间】:2017-12-28 08:27:04
【问题描述】:
我有一个名为 UploadFile 的 mvc 页面,其中包含他们的操作和视图。在我的操作中,我使用 TempData 从一个发送到另一个,以便在刷新视图时重复使用我的 excel 列表。从网格的第一页到第二页时它起作用了。但是在第二次刷新后,临时数据消失了,我又得到了一个空网格。
在传递另一个视图/操作之前,我如何保留和重复使用我的 TempData。
[HttpGet]
public ActionResult UploadFile()
{
return View("UploadFile", TempData["Veri"]);
}
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
try
{
string _FileName = string.Empty;
string _path = string.Empty;
List<ImportExcelDto> listExcel = new List<ImportExcelDto>();
if (file.ContentLength > 0)
{
_FileName = Path.GetFileName(file.FileName);
_path = Path.Combine(Server.MapPath("~/App_Data/uploads"), _FileName);
string tempfolder = Server.MapPath("~/App_Data/uploads");
var fileGeneration = new DirectoryInfo(Server.MapPath("~/App_Data/uploads"));
fileGeneration.GetFiles("*", SearchOption.AllDirectories).ToList().ForEach(f => f.Delete()); //Directory'deki eski excel dosyalarını temizler
file.SaveAs(_path);
}
ViewBag.Message = "Dosya Başarıyla Aktarıldı!";
DataTable dt = Helpers.GetDataTableFromExcel(_path, true);
for (int i = 0; i < dt.Rows.Count; i++)
{
ImportExcelDto item = new ImportExcelDto() { KartNo = dt.Rows[i][0].ToString(), Tutar = dt.Rows[i][1].ToDecimal() };
listExcel.Add(item);
}
var TempDataVeri = listExcel;
TempData["Veri"] = TempDataVeri;
return View("UploadFile", listExcel);
}
catch (Exception ex)
{
ViewBag.Message = "Dosya Aktarılamadı!";
return View();
}
}
【问题讨论】:
-
我很困惑为什么要多次使用临时数据?您可以在 post 函数之外提取对象实例化,但这会通过验证过程,并且多个客户端可能会混淆相同的对象。我认为您需要先告诉我们了解您的最终目标,然后我才能回答这个问题。
标签: c# asp.net-mvc asp.net-mvc-4