<p>Using ASP.NET MVC there are situations (such as form submission) that may require a <code>RedirectToAction</code>.  </p>

<p>One such situation is when you encounter validation errors after a form submission and need to redirect back to the form, but would like the URL to reflect the URL of the form, not the action page it submits to.</p>

<p>As I require the form to contain the originally <code>POST</code>ed data, for user convenience, as well as validation purposes, how can I pass the data through the <code>RedirectToAction()</code>?  If I use the viewData parameter, my <code>POST</code> parameters will be changed to <code>GET</code> parameters.</p>

[{'answer_id': 1940, 'author': 'Matt Mitchell', 'author_id': 364, 'author_profile': 'https://Stackoverflow.com/users/364', 'pm_score': 7, 'selected': True, 'text': '<p>The solution is to use the TempData property to store the desired Request components.</p>\n\n<p>For instance:</p>\n\n<pre><code>public ActionResult Send()\n{\n    TempData["form"] = Request.Form;\n    return this.RedirectToAction(a =&gt; a.Form());\n}\n</code></pre>\n\n<p>Then in your "Form" action you can go:</p>\n\n<pre><code>public ActionResult Form()\n{\n    /* Declare viewData etc. */\n\n    if (TempData["form"] != null)\n    {\n        /* Cast TempData["form"] to \n        System.Collections.Specialized.NameValueCollection \n        and use it */\n    }\n\n    return View("Form", viewData);\n}\n</code></pre>\n'}, {'answer_id': 4415, 'author': 'Haacked', 'author_id': 598, 'author_profile': 'https://Stackoverflow.com/users/598', 'pm_score': 5, 'selected': False, 'text': "<p>Keep in mind that TempData stores the form collection in session. If you don't like that behavior, you can implement the new ITempDataProvider interface and use some other mechanism for storing temp data. I wouldn't do that unless you know for a fact (via measurement and profiling) that the use of Session state is hurting you.</p>\n"}, {'answer_id': 27013, 'author': "Dane O'Connor", 'author_id': 1946, 'author_profile': 'https://Stackoverflow.com/users/1946', 'pm_score': 3, 'selected': False, 'text': '<p>There is another way which avoids tempdata. The pattern I like involves creating 1 action for both the original render and re-render of the invalid form. It goes something like this:</p>\n\n<pre><code>var form = new FooForm();\n\nif (request.UrlReferrer == request.Url)\n{\n     // Fill form with previous request\'s data\n}\n\nif (Request.IsPost())\n{\n     if (!form.IsValid)\n     {\n         ViewData["ValidationErrors"] = ...\n     } else {\n         // update model\n         model.something = foo.something;\n         // handoff to post update action\n         return RedirectToAction("ModelUpdated", ... etc);\n     }\n}\n\n// By default render 1 view until form is a valid post\nViewData["Form"] = form;\nreturn View();\n</code></pre>\n\n<p>That\'s the pattern more or less. A little pseudoy. With this you can create 1 view to handle rendering the form, re-displaying the values (since the form will be filled with previous values), and showing error messages.</p>\n\n<p>When the posting to this action, if its valid it transfers control over to another action.</p>\n\n<p>I\'m trying to make this pattern easy in the <a href="http://www.codeplex.com/ValidationFramework/" rel="noreferrer">.net validation framework</a> as we build out support for MVC.</p>\n'}, {'answer_id': 718653, 'author': 'Dan', 'author_id': 230, 'author_profile': 'https://Stackoverflow.com/users/230', 'pm_score': 4, 'selected': False, 'text': '<p>Take a look at <a href="https://mvccontrib.codeplex.com/" rel="nofollow noreferrer">MVCContrib</a>, you can do this:</p>\n\n<pre><code>using MvcContrib.Filters;\n\n[ModelStateToTempData]\npublic class MyController : Controller {\n    //\n    ...\n}\n</code></pre>\n'}, {'answer_id': 20298264, 'author': 'lzlstyle', 'author_id': 1049260, 'author_profile': 'https://Stackoverflow.com/users/1049260', 'pm_score': 2, 'selected': False, 'text': '<p>If you want to pass data to the redirected action, the method you could use is:</p>\n\n<pre><code>return RedirectToAction("ModelUpdated", new {id = 1});\n// The definition of the action method like  public ActionResult ModelUpdated(int id);\n</code></pre>\n'}, {'answer_id': 62451171, 'author': 'Aswal', 'author_id': 3560061, 'author_profile': 'https://Stackoverflow.com/users/3560061', 'pm_score': 0, 'selected': False, 'text': '<p>TempData is the solution which keeps the data from action to action.</p>\n\n<pre><code>Employee employee = new Employee\n                {\n                        EmpID = "121",\n                        EmpFirstName = "Imran",\n                        EmpLastName = "Ghani"\n                };\n                TempData["Employee"] = employee;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/1936', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/364/']