C# 带参数的 RedirectToAction

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1257482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:19:21  来源:igfitidea点击:

RedirectToAction with parameter

c#asp.net-mvccontrollerredirecttoaction

提问by Eric Brown - Cal

I have an action I call from an anchor thusly, Site/Controller/Action/IDwhere IDis an int.

我有一个动作,我从一个锚正是如此,调用Site/Controller/Action/ID其中ID的一个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 IDin tempdata, but when you hit f5 to refresh the page again after going back, the tempdata is gone and the page crashes.

有没有聪明的方法来做到这一点?目前我藏ID在临时数据中,但是当您返回后按 f5 再次刷新页面时,临时数据消失并且页面崩溃。

采纳答案by Eric Brown - Cal

Kurt's answershould be right, from my research, but when I tried it I had to do this to get it to actually work for me:

根据我的研究,Kurt 的答案应该是正确的,但是当我尝试它时,我必须这样做才能让它真正为我工作:

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

If I didn't specify the controller and the action in the RouteValueDictionaryit didn't work.

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

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.

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

If you are coming along later, try Kurt's answer first, and if you still have issues try this one.

如果您稍后来,请先尝试 Kurt 的回答,如果您仍有问题,请尝试这个。

回答by Kurt Schindler

You can pass the id as part of the routeValues parameter of the RedirectToAction() method.

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

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

This will cause a redirect to Site/Controller/Action/99. No need for temp or any kind of view data.

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

回答by Bahamut

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.

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

eg.

例如。

Global.asax

全球.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
        });
    }

then the parameters you'll need to pass will change to:

那么您需要传递的参数将更改为:

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

回答by aslanpayi

....

....

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

....

....

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

回答by Charles Philip

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:

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

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

回答by Rohit

MVC 4 example...

MVC 4 示例...

Note that you do not always have to pass parameter named ID

请注意,您不必总是传递名为 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();
} 

Of course you can assign string to model fields instead of using ViewBag if that is your preference.

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

回答by mateuscb

If your parameter happens to be a complex object, this solves the problem. The key is the RouteValueDictionaryconstructor.

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

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

If you happen to have collections, it makes it a bit trickier, but this other answer covers this very nicely.

如果您碰巧有收藏品,它会变得有点棘手,但是这个其他答案很好地涵盖了这一点

回答by kavitha Reddy

RedirectToActionwith parameter:

RedirectToAction带参数:

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

回答by chr.solr

If your need to redirect to an action outside the controller this will work.

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

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

回答by CF5

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.

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

e.g.

例如

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

So the url would be:

所以网址将是:

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

These can then be referenced by your controller:

然后这些可以被你的控制器引用:

public ActionResult ACTION(string id, string otherParam, string anotherParam) {
   // Your code
          }