是否可以“转移”从一个操作到另一个操作的 ASP.NET MVC 请求?
假设我有以下操作:
public ActionResult DoSomething()
{
// Some business logic
return RedirectToAction("AnotherAction", RouteData.Values);
}
public ActionResult AnotherAction(string name, int age)
{
...
}
以及以下表单:
<form method="post" action="DoSomething">
<input name="name" type="text" />
<input name="age" type="text" />
<input type="submit" value="Go" />
</form>
点击该表单上的提交将转到 DoSomething 操作,然后转到 AnotherAction - 传递所有相关值分为姓名和年龄。这是一种享受!
但是我显然无法访问AnotherAction中的任何其他提交的表单值,因为从DoSomething重定向时它们会丢失:
public ActionResult AnotherAction(string name, int age)
{
// This won't work
var other = Request.Form["SomeDynamicVariable"];
}
更理想的是TransferToAction > 方法,重新运行 MVC 引擎“想象”表单已发布到 AnotherAction:
return TransferToAction("AnotherAction");
我可以这样做吗?
如果这个功能不能开箱即用,那么我会制作它,在博客上发布它!
Suppose I have the following actions:
public ActionResult DoSomething()
{
// Some business logic
return RedirectToAction("AnotherAction", RouteData.Values);
}
public ActionResult AnotherAction(string name, int age)
{
...
}
And the following form:
<form method="post" action="DoSomething">
<input name="name" type="text" />
<input name="age" type="text" />
<input type="submit" value="Go" />
</form>
Hitting submit on that form will go to the DoSomething action, and in turn to AnotherAction - passing in all the relevant values into name and age. It works a treat!
But I obviously cannot access any other submitted form values in AnotherAction, because they are lost when redirecting from DoSomething:
public ActionResult AnotherAction(string name, int age)
{
// This won't work
var other = Request.Form["SomeDynamicVariable"];
}
What would be more ideal is a TransferToAction method, that re-runs the MVC engine "imagining" the form had been posted to AnotherAction instead:
return TransferToAction("AnotherAction");
Can I do this?
If this functionality is not available out-of-the-box, then I'll make it, blog it and post it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 TempData 构造来存储 Request.Form。 TempData 仅适用于给定的请求,因此处理完成后它将被清除。
Use the TempData construct to store the Request.Form. TempData is only around for the given request, so it will be cleared after the processing is finished.
您的控制器操作也是有效的公共函数
因此,当您现在从 AnotherAction 访问 Request 对象时,您可以执行此操作,
它仍然是相同的,因为您显然没有发出另一个请求。
your controller actions are also valid public functions
so you can do this
when you now access the Request object from AnotherAction it is still the same because you obviously didn't made another request.
执行此操作的一种方法是从第一个操作调用第二个操作并捕获响应。
这并不是一件小事,如此处讨论。
One way to do this would be to invoke the second action from the first action and capture the response.
This is not trivial, as discussed here.
您可以使用临时数据传递模型状态。拥有很少的 FilterAttributes 确实会简化该过程并且非常容易。
您应该阅读 http://ben.onfabrik.com/posts/ automatic-modelstate-validation-in-aspnet-mvc 用于正确使用过滤器属性。此外,该博客包含大量有关使用正确的 mvc 操作模型状态和 PRG 模式的信息。
这是一个更广泛的答案,不仅适合您的具体情况,而且是值得的。
You can pass modelstate using temp data. Having few FilterAttributes will really simplify the process and it's really easy.
You should read http://ben.onfabrik.com/posts/automatic-modelstate-validation-in-aspnet-mvc for proper use of filter attributes. Also that blog contains lots of information regarding to using proper mvc manipulation of modelstate and PRG pattern.
It's a broader answer not only for your specific case but it's worth it.