【问题标题】:ASP.NET MVC - Add Query String Value to RedirectToAction in ControllerASP.NET MVC - 将查询字符串值添加到控制器中的 RedirectToAction
【发布时间】:2017-03-23 09:43:10
【问题描述】:

我有一个 ASP.NET MVC 应用程序。我的应用程序正在使用 T4MVC。在我的控制器中,我需要从一个动作重定向到另一个动作。当我这样做时,我想附加一个查询字符串值。我可以在没有查询字符串值的情况下成功重定向,但我没有成功应用查询字符串值。我的操作如下所示:

[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Action1()
{                        
   return RedirectToAction(MVC.MyController.Action2().AddRouteValue("id", "5"));
}

[Route("action-2")]
public virtual ActionResult Action2(string input)
{
  ViewBag.Input = input;
  return View(viewModel);
}

当我访问./action-2 时,Action2 工作正常。我也可以成功地 POST 到Action1。但是,当重定向不起作用时。我在地址栏中注意到以下内容:

/MyController/id

为什么?我该如何解决?我只想重定向回Action2,但这次添加了查询字符串参数。我错过了什么?

【问题讨论】:

  • 为什么不直接return RedirectToAction("Action2", "MyController", new { id = 5});

标签: c# asp.net-mvc


【解决方案1】:

您需要通过操作中的名称(在本例中为“input”)指定参数才能使其工作,见下文:

return redirectToAction(MVC.MyController.Action2().AddRouteValue("input", "5"));

或者:

return RedirectToAction("Action2", "MyController", new { input = "myInput"});

【讨论】:

    【解决方案2】:

    我尝试以下方式,它对我来说很好。

    return RedirectToAction("Index", "CustomBuilder", new { usern = "admin" });
    

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2020-10-03
      • 2010-11-07
      • 2014-05-11
      相关资源
      最近更新 更多