C# 如何在 ASP.NET MVC 中 RedirectToAction 不丢失请求数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to RedirectToAction in ASP.NET MVC without losing request data
提问by Matt Mitchell
Using ASP.NET MVC there are situations (such as form submission) that may require a RedirectToAction
.
使用 ASP.NET MVC 在某些情况下(例如表单提交)可能需要RedirectToAction
.
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.
一种这样的情况是当您在提交表单后遇到验证错误并需要重定向回表单,但希望 URL 反映表单的 URL,而不是它提交到的操作页面。
As I require the form to contain the originally POST
ed data, for user convenience, as well as validation purposes, how can I pass the data through the RedirectToAction()
? If I use the viewData parameter, my POST
parameters will be changed to GET
parameters.
由于我要求表单包含原始POST
ed 数据,为了用户方便以及验证目的,我如何通过RedirectToAction()
? 如果我使用 viewData 参数,我的POST
参数将更改为GET
参数。
采纳答案by Matt Mitchell
The solution is to use the TempData property to store the desired Request components.
解决方案是使用 TempData 属性来存储所需的请求组件。
For instance:
例如:
public ActionResult Send()
{
TempData["form"] = Request.Form;
return this.RedirectToAction(a => a.Form());
}
Then in your "Form" action you can go:
然后在您的“表单”操作中,您可以:
public ActionResult Form()
{
/* Declare viewData etc. */
if (TempData["form"] != null)
{
/* Cast TempData["form"] to
System.Collections.Specialized.NameValueCollection
and use it */
}
return View("Form", viewData);
}
回答by Haacked
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.
请记住,TempData 将表单集合存储在会话中。如果您不喜欢这种行为,您可以实现新的 ITempDataProvider 接口并使用其他一些机制来存储临时数据。我不会这样做,除非你知道一个事实(通过测量和分析)使用 Session 状态正在伤害你。
回答by Dane O'Connor
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:
还有另一种避免临时数据的方法。我喜欢的模式涉及为无效表单的原始渲染和重新渲染创建 1 个操作。它是这样的:
var form = new FooForm();
if (request.UrlReferrer == request.Url)
{
// Fill form with previous request's data
}
if (Request.IsPost())
{
if (!form.IsValid)
{
ViewData["ValidationErrors"] = ...
} else {
// update model
model.something = foo.something;
// handoff to post update action
return RedirectToAction("ModelUpdated", ... etc);
}
}
// By default render 1 view until form is a valid post
ViewData["Form"] = form;
return View();
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.
或多或少就是这种模式。有点假。有了这个,您可以创建 1 个视图来处理呈现表单、重新显示值(因为表单将填充以前的值)并显示错误消息。
When the posting to this action, if its valid it transfers control over to another action.
当发布到这个动作时,如果它有效,它会将控制权转移到另一个动作。
I'm trying to make this pattern easy in the .net validation frameworkas we build out support for MVC.
回答by Dan
Take a look at MVCContrib, you can do this:
看看MVCContrib,你可以这样做:
using MvcContrib.Filters;
[ModelStateToTempData]
public class MyController : Controller {
//
...
}
回答by lzlstyle
If you want to pass data to the redirected action, the method you could use is:
如果要将数据传递给重定向操作,可以使用的方法是:
return RedirectToAction("ModelUpdated", new {id = 1});
// The definition of the action method like public ActionResult ModelUpdated(int id);