Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Html.ActionLink to specify an exact URL with MVC3

I have the following route:

        routes.MapRoute(
            "Power", // Route name
            "Power/{id}", // URL with parameters
            new 
            { 
                controller = "flood", 
                action = "index", 
                id = UrlParameter.Optional 
            }
        );

and the following address which I call:

<a href="/Power/"  >

Now I would like to do the above call with an Html.ActionLink like this:

@Html.ActionLink("xxx", 
                "index",
                "flood",
                new { "Power" },
                null 
                )

It seems not to work as I get an error "Invalid anonymous type declaration" where I have new { "Power" }. Can someone give me some advice and get me on the correct track.

I would also like to be able to call the following with another link:

<a href="/Power/001"  >`

thanks

ps. Please note I am using MVC3. I understand the syntax for this changed from version 1 > 2 > MVC3.

like image 361
Nile Avatar asked Nov 30 '25 23:11

Nile


1 Answers

Use a RouteLink instead of an ActionLink:

@Html.RouteLink("xxx", "Power", new { id = "123" })

or if you specify the controller and the action with ActionLink and based on your route definition order the proper route should be picked:

@Html.ActionLink("xxx", "index", "flood", new { id = "123" }, null)
like image 117
Darin Dimitrov Avatar answered Dec 02 '25 16:12

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!