开发者

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

开发者 https://www.devze.com 2023-03-15 17:20 出处:网络
I have the following route: routes.MapRoute( \"Power\", // Route name \"Power/{id}\", // URL with parameters

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.


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)


The problem is on the declaration of your parameters. This will work fine:

Without ID:

@Html.ActionLink("xxx", 
            "index",
            "flood")

With ID:

@Html.ActionLink("xxx", 
            "index",
            "flood",
            new { id =123 },
            null 
            )

You're declaring a anonymous type, without saying the name of the property, only the value.

0

精彩评论

暂无评论...
验证码 换一张
取 消