【问题标题】:Can I Redirect from Child Action Method to the New Action Method and return their view instead of returning to the parent of that child? [duplicate]我可以从子操作方法重定向到新操作方法并返回他们的视图而不是返回到该孩子的父级吗? [复制]
【发布时间】:2018-04-28 17:06:27
【问题描述】:

假设我有一个操作方法,例如 Pseudocode

Pseudo Code is here 这是否可以从子级重定向到另一个父级操作方法并返回他们的视图而不是将视图返回给该子级的父级?

【问题讨论】:

  • 请在问题中写下您的伪代码。您可以使用格式很好地显示它(带有缩进),方法是选择问题中的代码并点击 Ctrl+k

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-3


【解决方案1】:

您可以从任何操作返回任何您喜欢的响应。使用您的示例:

public ActionResult Method()
{
    return ChildActionMethod();
}

public ActionResult ChildActionMethod()
{
    return RedirectToAction("SomeAction");
}

这将返回来自Method 的重定向结果,因为它会返回ChildActionMethod 返回的任何内容。

值得注意的是,如果你在ChildActionMethod 中做return View(); 并且不指定视图,框架将使用当前请求来确定使用哪个视图。也就是说,默认情况下它将使用Method 的视图而不是ChildActionMethod。在这种情况下,您需要指定视图。

事实上,ChildActionMethod 甚至不需要是公共的“动作方法”。它可以是任何返回ActionResult 的方法。此时您只处理 C# 语言的基础知识(方法、类型等),而不是 ASP.NET MVC 框架。

【讨论】:

    猜你喜欢
    • 2020-08-17
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2018-01-26
    相关资源
    最近更新 更多