Ian Vink
Ian Vink

Reputation: 68810

ASP.NET MVC 4 Forcing Html.ActionLink to use a Parameter

I have a link created like this:

Html.ActionLink("Holdings", "Index", "Holdings", new { id = greenbaby }, null)

it renders a link:

http://blah/Holdings/Index/greenbaby 

I need

http://blah/Holdings/Index/?id=greenbaby 

or

http://blah/Holdings/?id=greenbaby 

Is there a way to get the ActionLink to do that?

Upvotes: 0

Views: 1372

Answers (1)

Andrey Gubal
Andrey Gubal

Reputation: 3479

You may add special route for it:

//*returns blah/Holdings/?id=greenbaby 
    routes.MapRoute(
                    name: "MyRoute",
                    url: "blah/{controller}/?id={id}",
                    defaults: new { controller = "Holdings", action = "Index", id = UrlParameter.Optional }
                );


//*returns blah/Holdings/Index/?id=greenbaby 
        routes.MapRoute(
                        name: "MyRoute",
                        url: "blah/{controller}/{action}/?id={id}",
                        defaults: new { controller = "Holdings", action = "Index", id = UrlParameter.Optional }
                    );

Upvotes: 1

Related Questions