I have a mvc page that named UploadFile with their actions and view. In my actions, I used TempData to send from one to another for reusing of my excel list when the view is refreshed. It worked when going from the first page to the second page of grid. However after second refresh, tempdata disappeared and I got an empty grid again.
How can I keep and reuse my TempData until pass another view/action.
[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();
}
}