【问题标题】:Redirect to Index Method having no parameters重定向到没有参数的索引方法
【发布时间】:2014-08-31 08:21:18
【问题描述】:

请帮助我,在我的控制器中,我有 2 个名称相同的操作方法,名为 "Index",但参数不同..

 public ActionResult Index()
        {...}

 [HttpPost] 

 public ActionResult Index(PickingMobile mi, string hdnSelectOperation, string btnMPSearch, string btnSearchMP)
        {...}

现在我想从其他动作重定向到没有参数的索引动作

public ActionResult ConfirmBatchPicking(PickingMobile DirectPicking)
        {
...
 return RedirectToAction("Index", "ManualPickingSearch");// from here I need to redirect to first Index Method which does not have any parameters
          }

但是当我保留断点并调试序列时,第一个 Index 方法没有被命中。请帮助我,如何重定向到第一个索引操作。

【问题讨论】:

  • 由于您的 Index 方法之一是 HttpPost 类型,所以当您调用时返回 RedirectToAction("Index", "ControllerName")。这会将您重定向到 HttpGet 方法
  • 但在调试时没有点击第一个 Index 方法

标签: asp.net-mvc asp.net-mvc-4 indexing redirecttoaction actionmethod


【解决方案1】:
  1. RedirectToAction 将 RedirectToRouteResult 返回到浏览器 回复。此响应使浏览器向 另一种操作方法(在您的情况下是索引操作) ManualPickingSearchcontroller)
  2. 您的第二个 Index 操作(一个带参数的操作)附加了 httpPost 属性。这意味着只能对其发出 post 请求。

所以,当你说 return RedirectToAction("Index", "ManualPickingSearch");默认情况下,它将重定向到第一个操作方法。它是默认行为。它应该如何工作。请检查您是否缺少某些东西。(检查控制器名称)

【讨论】:

  • 当然,因为我有查看包,因为它没有显示在查看页面中,所以只有我有疑问
  • 如果您想保持价值,请在重定向后使用 tempbag。
【解决方案2】:

试试这个;

return RedirectToAction("Index");

【讨论】:

  • OK,如果你使用RedirectToAction("Index", "ManualPickingSearch");,它将重定向到ManualPickingSearch控制器的Index方法。谢谢!
  • 是的,我只想重定向到 ManualPIckingSearch 中的 Index 方法。但问题是我有 2 个索引方法
  • 如果要在同一个控制器中调用方法,则不需要输入控制器名称。只要您不将[HttpPost] 放在您的第一个Index 方法中,就不会出现问题。
  • 有不同参数的Index方法(不是[HttpPost]一个)吗?
  • 是的,我有一个没有参数的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 2016-03-23
  • 2018-03-07
  • 1970-01-01
  • 2014-07-06
  • 2014-07-29
相关资源
最近更新 更多