开发者

ASP.NET MVC ActionLink help

I have the following action link in a view for shows at for example /Shows/Details/1

<%= Html.ActionLink(item.venue, "De开发者_JAVA技巧tails", "Venues", new { name = item.venue }) %>

This displays the name of the Venue the show is being played at, but I need it to link to the Venue instead of the Shows controller as at the moment the url it is creating is /Shows/Details?Length=6 when it needs to go to /Venues/Details?Name=VenueName

The name of the controller for Venues is VenuesController.

Thanks


You are using the wrong overload for the .ActionLink. Try this instead...

<%= Html.ActionLink(item.venue, "Details", "Venues", new { name = item.venue }, new {}) %>

It is currently selecting string, string, object, object overload. Your "Venues" string is being used for routing data.


Add this route to your global.asax:

routes.MapRoute(
    "Venue",
    "Venues/{name}",
    new { controller = "Venues", action = "Details", name = UrlParameter.Optional }
);

Then call it like this:

<%= Html.ActionLink(item.venue, "Details", "Venues", new { name = item.venue }, new{}) %>

it will render:

/Venue/NameOfTheVenue


I know it's not the answer to your question but it will help you build better application. I suggest you take a look at T4MVC : http://mvccontrib.codeplex.com/wikipage?title=T4MVC it will help you a lot to build your link and get ripped off magic string. I use it for over a year and would never go back. There's also a nice video on Channel 9 : http://channel9.msdn.com/blogs/jongalloway/jon-takes-five-with-david-ebbo-on-t4mvc

Hope it helps!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜