views:

48

answers:

2

If I do not want to do this in my View Markup,

<%=Html.RouteLink("Listings", "Listings", new {market = "Austin", state = "Texas", pagenumber = 3 })%>

what would be the best approach to creating links in MVC 2? I have a HTML helper to build a custom link but all it is doing is acting as a wrapper around RouteLink (or ActionLink). I would want something more elegant and one that has a shorter syntax. If I build my links in a controller, how do I push it into the view? Via ViewData? (hope not). Or would it be part of the ViewModel?

+2  A: 

Long term, I think the Html.ActionLink which you see in the MVC Futures library is the way to fly:

<%= Html.ActionLink<ListingsController>(x => x.Listings("Austin", "Texas", 3)) %>

Using your HtmlHelpers, even if they are just wrappers around the current magic-string based functionality as it at least reduces your frontage to magic strings and provides a single point to change.

I wouldn't get involved in building links on the back-end if I could help it in an effort to make syntactical sugar.

Wyatt Barnett
@Wyatt. This is awesome. I think I'm getting the drift now. One of the reasons that I was planning on using the ViewModel was testability. Any ideas on how to test these links?
Praveen
The ActionLink method in the Futures library has some serious performance issues.Source: http://www.chadmoran.com/blog/2009/4/23/optimizing-url-generation-in-aspnet-mvc-part-2.html
Praveen
Those tests are over a year old, running on an older version of MVC and an older version of The Framework. So I'd ask if they are still accurate.
Wyatt Barnett
+2  A: 

I like using T4MVC better than MVC Futures ActionLink helpers. You line would look like this:

<%:Html.ActionLink("Listings", MVC.Listings.Index("Austing", "Texas", 3)) %>

http://mvccontrib.codeplex.com/wikipage?title=T4MVC

Necros
@Necros,I'm not sure if this is even a valid question :D But, does T4MVC support non-MVC-standard project structures? I have my Views and Partial Views folders in a different location than the default location in an MVC 2 project. I have a custom view engine that picks the right view depending on various business rules.
Praveen
@Praven - Yes, there is a setting file you get with the template, where you can set the directory for your views and controllers. If it's more complicated then just setting the name of the directory, you'd have to modify the t4 template itself.
Necros
Excellent. I'm playing with it right now. Loving it so far. Thank you!
Praveen
This is working out great for me.I tweaked the settings file to point it to my custom Views and Controllers folders. I had to set the ControllersFolder property as shown below in order for it to work.@"AppCode\Controllers"Another thing I'd love for David to add to T4 is a custom folder to specify PartialViews. Right now, I think it looks in the main Views folder.
Praveen