It's a syntactic thing and you're correct - if you specify "productname" in your Route you need to pass a variable named "productname". So you'd need to update your link thus:
It's actually done via the Route object, which the helper uses. The Route instance is part of the url routing set of classes. When you try to generate a route url, we look at which routes you've configured match the parameters you specify and we generate
the first one that matches.
Phil Haack (http://haacked.com/)
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
Marked as answer by Haacked on Dec 13, 2007 04:59 PM
c0s
0 Points
4 Posts
Routes and ActionLink
Dec 12, 2007 12:01 AM|LINK
One of the benefits to using ActionLink is so that I can change the format of my URL and have all my links update automatically, I get that.
But if I have "[controller]/[action]/[id]" setup as a route and use
Html.ActionLink("Something", new { action = "Edit", controller = "Products", id = p.productid })
then I change the route to "[controller]/[action]/[productname]" this won't render correctly because ActionLink doesn't know how to set "productname".
Am I missing something or am I to add every property to the anonymous object? And if so, what happens if I add properties to the Products class?
Can I pass in the object and have the ActionLink automatically set fields in the URL to corresponding properties in the object?
If I can't and I wanted to add this functionality, would I create a RouteHandler or would this be a helper method issue?
Thanks.
Gabe
ASP.NET MVC CTP MVC
robconery
Participant
852 Points
195 Posts
Re: Routes and ActionLink
Dec 12, 2007 03:37 AM|LINK
It's a syntactic thing and you're correct - if you specify "productname" in your Route you need to pass a variable named "productname". So you'd need to update your link thus:
Html.ActionLink("Link", new{action="Edit", controller="Products",productname="Chang's Ice Tea"})
But that might not be the best way to do it. You can also rewrite this link:
Html.ActionLink<ProductController>(x=>x.Edit(p.ProductName))
The benefit here is that you get a compiler error that complains if you're not passing the right data.
c0s
0 Points
4 Posts
Re: Routes and ActionLink
Dec 12, 2007 04:09 AM|LINK
So I guess my question now would be: is the URL creation done in the helper or the routehandler?
Haacked
Contributor
6901 Points
412 Posts
Re: Routes and ActionLink
Dec 12, 2007 05:49 AM|LINK
It's actually done via the Route object, which the helper uses. The Route instance is part of the url routing set of classes. When you try to generate a route url, we look at which routes you've configured match the parameters you specify and we generate the first one that matches.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?