2
votes

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();
            }
       }
2
I'm confused why you want to use temp data multiple times? You could pull out the object instantiation outside the post function, but that would by pass the verification process and multiple clients could get the same objects mixed up. I think you need to tell us understand your end goal before I can answer the question. - dev8989

2 Answers

6
votes

Use KEEP and PEEK

example :

object value = TempData.Peek("value");

object value = TempData["value"];
//later on decide to keep it
TempData.Keep("value");

This will help you to keep the data for more than 1 access/request.

Usually Once you access the TempData it gets deleted

You can use Peek when you always want to retain the value for another request. Use Keep when retaining the value depends on additional logic.

0
votes

TempData keeps the information for the time of an HTTP Request. This mean only from one page to another. This also works with a 302/303 redirection because it’s in the same HTTP Request. It helps to maintain data when you move from one controller to other controller or from one action to other action.

You can use Session to keep the value for long time.

Session["Veri"] = TempDataVeri;