代码之家  ›  专栏  ›  技术社区  ›  Eric Brown - Cal

带参数的重定向操作

  •  504
  • Eric Brown - Cal  · 技术社区  · 17 年前

    我有一个动作我从一个主播那里召唤, Site/Controller/Action/ID 哪里 ID 是一个 int .

    身份证件 在tempdata中,但是当 返回后,按f5再次刷新页面,tempdata消失,页面崩溃。

    14 回复  |  直到 8 年前
        1
  •  1066
  •   Elijah66    17 年前

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

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

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

        2
  •  209
  •   Community Mohan Dere    9 年前

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

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

    如果我没有在 RouteValueDictionary 它不起作用。

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

        3
  •  73
  •   pb2q    10 年前

    RedirectToAction 带参数:

    return RedirectToAction("Action","controller", new {@id=id});
    
        4
  •  67
  •   CF5    9 年前

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

    例如

    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
              }
    
        5
  •  45
  •   Thivan Mydeen    10 年前
    //How to use RedirectToAction in MVC
    
    return RedirectToAction("actionName", "ControllerName", routevalue);
    

    例子

    return RedirectToAction("Index", "Home", new { id = 2});
    
        6
  •  42
  •   Rohit    13 年前

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

        7
  •  27
  •   Community Mohan Dere    9 年前

    如果您的参数恰好是一个复杂对象, this solves the problem . 关键是 RouteValueDictionary

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

    如果你碰巧有收藏,那就有点麻烦了, but this other answer covers this very nicely .

        8
  •  27
  •   Zaeem Q    7 年前
    RedirectToAction("Action", "Controller" ,new { id });
    

    new{id = id}

    我正在重定向到同一控制器中,因此不需要 "Controller" 但我不确定当需要控制器作为参数时,背后的具体逻辑。

        9
  •  20
  •   Toriq Sagor    9 年前

    如果要为显示错误消息 [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 });
                }
    }
    

    希望它能正常工作。

        10
  •  17
  •   Andrea    12 年前

    ....

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

    return RedirectToAction("ActionName", new { Id = parameter });
    
        11
  •  16
  •   Charles Philip    13 年前

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

    return RedirectToAction(actionName: "Action", routeValues: new { id = 99 });
    
        12
  •  9
  •   chr.solr    11 年前

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

    return RedirectToAction("ACTION", "CONTROLLER", new { id = 99 });
    
        13
  •  8
  •   Bahamut    15 年前

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

    如。

    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 } ) );
    
        14
  •  3
  •   AsIndeed    5 年前

    这一行代码就可以做到这一点:

    return Redirect("Action"+id);
    
        15
  •  2
  •   mikemay    8 年前

    [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
    }