1

I have controller which gets list of keys and i want to pass them to another controller action:

 public class DocumentController : Controller
    {
        private  List<DocumentKey> _keys = new List<DocumentKey>();

        [HttpPost]
        public ActionResult Send(Document doc, IEnumerable<HttpPostedFileBase> file)
        {
            ...

            _keys = getKeys();

            return RedirectToAction("Status", "Home", _keys);
        }

i try to use

return RedirectToAction("Status", "Home", _keys); 

and

return RedirectToAction("Status", "Home", new {keys = _keys});

But in Status keys is always null or has count = 0.

public class HomeController : Controller
    {

        public ActionResult Status(List<DocumentKey> keys)
        {
            return View(keys);
        }

I can pass simple data, for example:

 return RedirectToAction("Status", "Home", new {key =  _keys.First().ToString()});
 public ActionResult Status(string key)

this works, but is there a way to pass collection ?

2 Answers 2

2

Passing this type of data as query string parameters will not work as you are attempting to do so. If you need data to persist until your next request, it may be feasible to push the "keys" into tempdata.

public class DocumentController : Controller
    {
        private  List<DocumentKey> _keys = new List<DocumentKey>();

        [HttpPost]
        public ActionResult Send(Document doc, IEnumerable<HttpPostedFileBase> file)
        {
            ...
            TempData["_keys"] = getKeys();
            return RedirectToAction("Status", "Home");
        }

public class HomeController : Controller
    {

        public ActionResult Status()
        {
            List<DocumentKey> keys = TempData["_keys"];
            return View(keys);
        }
Sign up to request clarification or add additional context in comments.

2 Comments

TempData will store until i refresh "Status" page?
@andronz - correct - tempdata is stored between two successive requests, after that it is destroyed.
2

RedirectToAction does a round trip by sending a HTTP 302 to the client. Is that really what you need?

Because if you don't, then the easiest would be to call your HomeController's Status method directly. You would also need to replace your call to View to explicitly specify the name of the view, i.e:

    public ActionResult Status(List<DocumentKey> keys)
    {
       return View("Status", keys);
    }

If you do need the round trip, then a solution would be to use TempData to store your data as suggested by Jesse.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.