带参数的 RedirectToAction
我有一个从锚点调用的操作,Site/Controller/Action/ID
,其中ID
是一个int
。
稍后我需要从控制器重定向到相同的操作。
有什么聪明的方法可以做到这一点吗? 目前我正在 tempdata 中存储 ID
,但是当你 返回后再次按f5刷新页面,临时数据消失,页面崩溃。
I have an action I call from an anchor thusly, Site/Controller/Action/ID
where ID
is an int
.
Later on I need to redirect to this same Action from a Controller.
Is there a clever way to do this? Currently I'm stashing ID
in tempdata, but when you
hit f5 to refresh the page again after going back, the tempdata is gone and the page crashes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您可以将 id 作为 RedirectToAction() 方法的routeValues 参数的一部分传递。
这将导致重定向到 Site/Controller/Action/99。 不需要临时数据或任何类型的视图数据。
You can pass the id as part of the routeValues parameter of the RedirectToAction() method.
This will cause a redirect to Site/Controller/Action/99. No need for temp or any kind of view data.
根据我的研究,Kurt 的答案应该是正确的,但是当我尝试时,我必须这样做才能使其真正发挥作用对我来说:
如果我没有在 RouteValueDictionary 中指定控制器和操作,它就不起作用。
此外,当这样编码时,第一个参数(Action)似乎被忽略。 所以如果你只是在Dict中指定控制器,并期望第一个参数指定Action,那么它也不起作用。
如果您稍后再来,请先尝试 Kurt 的答案,如果仍有问题,请尝试这个。
Kurt's answer should be right, from my research, but when I tried it I had to do this to get it to actually work for me:
If I didn't specify the controller and the action in the
RouteValueDictionary
it didn't work.Also when coded like this, the first parameter (Action) seems to be ignored. So if you just specify the controller in the Dict, and expect the first parameter to specify the Action, it does not work either.
If you are coming along later, try Kurt's answer first, and if you still have issues try this one.
还值得注意的是,您可以传递多个参数。 id 将用于构成 URL 的一部分,其他任何内容将作为 ? 之后的参数传递。 在 url 中,默认情况下将进行 UrlEncoded。
例如,
因此 url 将是:
然后您的控制器可以引用这些:
It is also worth noting that you can pass through more than 1 parameter. id will be used to make up part of the URL and any others will be passed through as parameters after a ? in the url and will be UrlEncoded as default.
e.g.
So the url would be:
These can then be referenced by your controller:
RedirectToAction
带有参数:RedirectToAction
with parameter:例子
example
MVC 4 示例...
请注意,您不必总是传递名为 ID 的参数
,当然,
如果您愿意的话,您可以将字符串分配给模型字段,而不是使用 ViewBag。
MVC 4 example...
Note that you do not always have to pass parameter named ID
And,
Of course you can assign string to model fields instead of using ViewBag if that is your preference.
如果您的参数恰好是一个复杂对象,这可以解决问题。 关键是
RouteValueDictionary
构造函数。如果您碰巧有集合,那就有点棘手了,但另一个答案很好地涵盖了这一点。
If your parameter happens to be a complex object, this solves the problem. The key is the
RouteValueDictionary
constructor.If you happen to have collections, it makes it a bit trickier, but this other answer covers this very nicely.
为我工作,不需要做
new{id = id}
我重定向到同一个控制器内,所以我不需要
"Controller"
但我不确定何时需要控制器作为参数时背后的具体逻辑。Worked for me, didn't need to do
new{id = id}
I was redirecting to within the same controller so I didn't need the
"Controller"
but I'm not sure on the specific logic behind when the controller is required as a parameter.如果有人想显示
[httppost]
的错误消息,那么他/她可以尝试通过传递一个用于详细信息的
ID,希望它能正常工作。
If one want to Show error message for
[httppost]
then he/she can try by passing an ID usingfor Details like this
Hope it works fine.
我也遇到了这个问题,如果您在同一个控制器中,一个很好的方法是使用命名参数:
I had this issue as well, and quite a nice way to do it if you are within the same controller is to use named parameters:
如果您需要重定向到控制器外部的操作,这将起作用。
If your need to redirect to an action outside the controller this will work.
这可能是几年前的事了,但无论如何,这也取决于您的 Global.asax 地图路线,因为您可以添加或编辑参数以满足您的需求。
例如。
Global.asax
那么您需要传递的参数将更改为:
This might be years ago but anyways, this also depends on your Global.asax map route since you may add or edit parameters to fit what you want.
eg.
Global.asax
then the parameters you'll need to pass will change to:
这行代码就可以做到这一点:
This one line of code will do it:
以下使用asp.net core 2.1成功。 它可能适用于其他地方。 字典 ControllerBase.ControllerContext.RouteData.Values 可以在操作方法中直接访问和写入。 也许这就是其他解决方案中数据的最终目的地。 它还显示默认路由数据的来源。
The following succeeded with asp.net core 2.1. It may apply elsewhere. The dictionary ControllerBase.ControllerContext.RouteData.Values is directly accessible and writable from within the action method. Perhaps this is the ultimate destination of the data in the other solutions. It also shows where the default routing data comes from.
您也可以像这样发送任何 ViewBag、ViewData..
Also you can send any ViewBag, ViewData.. like this