是否可以“转移”从一个操作到另一个操作的 ASP.NET MVC 请求?

发布于 2024-08-03 04:52:21 字数 1038 浏览 5 评论 0原文

假设我有以下操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坏尐絯℡ 2024-08-10 04:52:21

使用 TempData 构造来存储 Request.Form。 TempData 仅适用于给定的请求,因此处理完成后它将被清除。

public ActionResult DoSomething()
{
    // Some business logic
    TempData["PostedFormValues"] = Request.Form;
    return RedirectToAction("AnotherAction", RouteData.Values);
}

public ActionResult AnotherAction(string name, int age)
{
   ...
   if (TempData["PostedFormValues"] != null)
   {
       //process here
   }
}

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.

public ActionResult DoSomething()
{
    // Some business logic
    TempData["PostedFormValues"] = Request.Form;
    return RedirectToAction("AnotherAction", RouteData.Values);
}

public ActionResult AnotherAction(string name, int age)
{
   ...
   if (TempData["PostedFormValues"] != null)
   {
       //process here
   }
}
锦爱 2024-08-10 04:52:21

您的控制器操作也是有效的公共函数
因此,当您现在从 AnotherAction 访问 Request 对象时,您可以执行此操作,

public ActionResult DoSomething(){    
// Some business logic    
// Get Params from Request
      return AnotherAction(name, age);
}

public ActionResult AnotherAction(string name, int age){
   ...
}

它仍然是相同的,因为您显然没有发出另一个请求。

your controller actions are also valid public functions
so you can do this

public ActionResult DoSomething(){    
// Some business logic    
// Get Params from Request
      return AnotherAction(name, age);
}

public ActionResult AnotherAction(string name, int age){
   ...
}

when you now access the Request object from AnotherAction it is still the same because you obviously didn't made another request.

_蜘蛛 2024-08-10 04:52:21

执行此操作的一种方法是从第一个操作调用第二个操作并捕获响应。
这并不是一件小事,如此处讨论

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.

弥枳 2024-08-10 04:52:21

您可以使用临时数据传递模型状态。拥有很少的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文