我有一个动作,我从一个锚调用,因此,站点/控制器/动作/ID,其中ID是一个int。

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

有什么聪明的办法吗?目前我在tempdata中存储ID,但当你 返回后,按f5再次刷新页面,tempdata消失,页面崩溃。


当前回答

值得注意的是,您可以传递多个参数。id将用来构成URL的一部分,任何其他将通过参数后?在url中,并将UrlEncoded为默认值。

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
          }

其他回答

MVC 4的例子…

注意,您并不总是必须传递名为ID的参数

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

And,

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

当然,你可以将字符串分配给模型字段,而不是使用ViewBag,如果这是你的偏好。

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

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

如果你碰巧有集合,这就有点棘手了,但另一个答案很好地涵盖了这个问题。

值得注意的是,您可以传递多个参数。id将用来构成URL的一部分,任何其他将通过参数后?在url中,并将UrlEncoded为默认值。

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
          }

RedirectToAction带参数:

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

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

我重定向到同一个控制器内,所以我不需要“控制器”,但我不确定当控制器被要求作为参数时背后的具体逻辑。