1

I have two actions in a controller. One that displays a form for file upload and another one that displays the results of the upload. I have created a POCO called FileInfo i.e

public class FileInfo
    {
        public string Name { get; set; }
        public int Length { get; set; }
        public string FileType { get; set; }
        public string ErrorMessage { get; set; }

    }

When I submit the form, the Upload action creates and populates the FileInfo object and then redirects to the second action called results. I want to be able to use the same file info object in the results action.

I am able to get around this using TemPData[], but it is limited since it only holds object data for a single request. I presume there must be a better way to share abjects between controller actions.Any help is appreciated!

// Upload Action

 List<FileInfo> fileInfo= new List<FileInfo>();
//populate  the fileInfo object using fi.Add()

if ((status.ToString() == "OK"))
             {
                 TempData["Info"] = fileInfo;
                 return RedirectToAction("Results");

             }
             else
             {
                 return RedirectToAction("Index");
             }

//Results action.

public ActionResult Results()
        {
            List<FileInfo> fi = TempData["Info"] as List<FileInfo>;
            if (fi != null)
            {
                return View(fi);
            }
            else
            {
                return View("Index");
            }
        }
lacoder
  • 1,253
  • 1
  • 11
  • 22
  • 1
    You are storing an instance of `FileInfo` in `TempData`, but trying to retrieve it as `List`. You will alwasy get `null`. – Igor Dec 19 '12 at 14:47
  • Possible duplicate of http://stackoverflow.com/questions/3363842/asp-net-mvc-redirect-to-action-need-to-pass-data – Heretic Monkey Dec 19 '12 at 14:48
  • Hi Igor, I have edited the code. I Instantiate the fileInfo object in the upload action, pass it into TempData and then use TempData in the results action. This works but I'm wondering if there is a better way around it . – lacoder Dec 19 '12 at 14:56
  • Why are you doing `status.ToString() == "OK"`? I'm guessing `status` is an `enum` so you would be better off doing `status == Status.OK` – Trevor Pilley Dec 19 '12 at 15:14
  • Thanks Trevor! I'm checking `HttpStatusCode` I just realised that I can do it like this: `status == HttpStatusCode.OK` – lacoder Dec 19 '12 at 15:25

1 Answers1

0

If you need something to stick around longer then one subsequent request, you will have to put it in Session or in persistent storage (e.g. database).

Igor
  • 15,833
  • 1
  • 27
  • 32