RedirectToAction with parameter

后端 未结 14 1807
傲寒
傲寒 2020-11-22 08:56

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 th

14条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 09:48

    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

        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 } ) );
    

提交回复
热议问题