views:

375

answers:

2

anybody knows how to make a method (I will put it in a extensions class) that will do the same as the mvc's RedirectToAction only using expressions (no magic strings).

So that instead of writing something like this:

 RedirectToAction("Detail", 
    new RouteValueDictionary { {"messageId", messageId}});

I would do like this:

this.RedirectToAction(x => x.Detail(messageId));

I tried and did something like this but I don't know how to add the parameters:

 public static RedirectToRouteResult RedirectToAction<T>(this T controller,
     Expression<Action<T>> expression)
 {
     return RedirectToAction(
        (expression.Body as MethodCallExpression).Method.Name
     );
 }
+1  A: 

http://www.clariusconsulting.net/blogs/kzu/archive/2008/04/07/59274.aspx

Also, I believe they moved type-safe HtmlHelper extensions into the MVC Futures assembly back when 1.0 came out, however I can't seem to find it now.

Will
+3  A: 

MvcContrib has it: http://mvccontrib.codeplex.com/wikipage?title=RedirectToAction

queen3
Aaaah, contrib not futures. +1
Will
this contrib is on mvc2, and I'm still using mvc 1
Omu
MVC1 release is also there, for example http://mvccontrib.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=33401.
queen3