带有参数的重定向ToAction

因此,我有一个从锚点调用的操作,Site/Controller/Action/ID其中IDint

稍后,我需要从控制器重定向到相同的操作。

有没有一个聪明的方法来做到这一点?目前我在坦普数据中隐藏ID,但是当你 返回后点击f5再次刷新页面,temdata不见了,页面崩溃。

983568 次浏览

您可以将id作为ReDirectToAction()方法的routeValue参数的一部分传递。

return RedirectToAction("Action", new { id = 99 });

这将导致重定向到Site/Controller/Action/99。不需要临时或任何类型的视图数据。

从我的研究来看,EYZ0应该是对的,但是当我尝试它时,我必须这样做才能让它真正为我工作:

return RedirectToAction( "Main", new RouteValueDictionary(
new { controller = controllerName, action = "Main", Id = Id } ) );

如果我没有在RouteValueDictionary中指定控制器和操作,它就不起作用。

同样,当这样编码时,第一个参数(Action)似乎被忽略了。因此,如果您只是在DICT中指定控制器,并期望第一个参数指定Action,它也不起作用。

如果你以后再来,先试试库尔特的答案,如果你还有问题,试试这个。

这可能是几年前的事了,但无论如何,这也取决于你的Global.asax地图路线,因为你可以添加或编辑参数来适应你想要的。

eg.

Global.asax

    public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
//new { controller = "Home", action = "Index", id = UrlParameter.Optional
new { controller = "Home", action = "Index", id = UrlParameter.Optional,
extraParam = UrlParameter.Optional // extra parameter you might need
});
}

然后你需要传递的参数将更改为:

return RedirectToAction( "Main", new RouteValueDictionary(
new { controller = controllerName, action = "Main", Id = Id, extraParam = someVariable } ) );
....


int parameter=Convert.ToInt32(Session["Id"].ToString());


....


return RedirectToAction("ActionName", new { Id = parameter });

我也遇到了这个问题,如果你在同一个控制器中,一个很好的方法是使用命名参数:

return RedirectToAction(actionName: "Action", routeValues: new { id = 99 });

MVC 4示例…

请注意,您不必始终传递名为ID的参数

var message = model.UserName + " - thanks for taking yourtime to register on our glorious site. ";
return RedirectToAction("ThankYou", "Account", new { whatever = message });

而且,

public ActionResult ThankYou(string whatever) {
ViewBag.message = whatever;
return View();
}

当然,如果您喜欢,您可以将字符串分配给模型字段而不是使用ViewBag。

如果您的参数恰好是一个复杂对象,这就解决了问题。关键是RouteValueDictionary构造函数。

return RedirectToAction("Action", new RouteValueDictionary(Model))

如果你碰巧有收藏,这会让它有点棘手,但另一个答案很好地说明了这一点

RedirectToAction带参数:

return RedirectToAction("Action","controller", new {@id=id});

如果您需要重定向到控制器之外的操作,这将起作用。

return RedirectToAction("ACTION", "CONTROLLER", new { id = 99 });

还值得注意的是,您可以传递多个参数。id将用于构成URL的一部分,任何其他参数将作为参数传递?在url中,默认为UrlEncode。

e. g.

return RedirectToAction("ACTION", "CONTROLLER", new {
id = 99, otherParam = "Something", anotherParam = "OtherStuff"
});

所以url应该是:

    /CONTROLLER/ACTION/99?otherParam=Something&anotherParam=OtherStuff

然后这些可以由您的控制器引用:

public ActionResult ACTION(string id, string otherParam, string anotherParam) {
// Your code
}
//How to use RedirectToAction in MVC


return RedirectToAction("actionName", "ControllerName", routevalue);

示例

return RedirectToAction("Index", "Home", new { id = 2});

如果想要显示[httppost]的错误消息,那么他/她可以尝试通过使用

return RedirectToAction("LogIn", "Security", new { @errorId = 1 });

像这样的细节

 public ActionResult LogIn(int? errorId)
{
if (errorId > 0)
{
ViewBag.Error = "UserName Or Password Invalid !";
}
return View();
}


[Httppost]
public ActionResult LogIn(FormCollection form)
{
string user= form["UserId"];
string password = form["Password"];
if (user == "admin" && password == "123")
{
return RedirectToAction("Index", "Admin");
}
else
{
return RedirectToAction("LogIn", "Security", new { @errorId = 1 });
}
}

希望它能正常工作。

以下内容在asp.net2.1核心中获得成功。它可能适用于其他地方。字典ControlllerBase. ControlllerContext. RouteData. Value可以从action方法中直接访问和写入。也许这是其他解决方案中数据的最终目的地。它还显示了默认路由数据的来源。

[Route("/to/{email?}")]
public IActionResult ToAction(string email)
{
return View("To", email);
}
[Route("/from")]
public IActionResult FromAction()
{
ControllerContext.RouteData.Values.Add("email", "mike@myemail.com");
return RedirectToAction(nameof(ToAction));
// will redirect to /to/mike@myemail.com
}
[Route("/FromAnother/{email?}")]`
public IActionResult FromAnotherAction(string email)
{
return RedirectToAction(nameof(ToAction));
// will redirect to /to/<whatever the email param says>
// no need to specify the route part explicitly
}
RedirectToAction("Action", "Controller" ,new { id });

为我工作,不需要做new{id = id}

我重定向到同一个控制器中,所以我不需要"Controller",但我不确定何时需要控制器作为参数的具体逻辑。

这一行代码将做到这一点:

return Redirect("Action"+id);

您也可以发送任何ViewBag,ViewData…像这样

return RedirectToAction("Action", new { Dynamic = ViewBag.Data= "any data" });