tags:

views:

863

answers:

3

Hi, I think there is no overload available to add parameters other than the action parameters list while creating actionlink through strongly typed action links. What I want is to add extra parameters which will be available in querystring .
For example with action MyAction(int id) in controller MyController. Html.ActionLink(mc=>mc.MyAction(5),"My Action") will produce link something like MyController/MyAction/5 but what I want is append querystring like this. MyController/MyAction/5?QS=Value. Is there any way,using strongly typed actionlinks, to achieve this.

+1  A: 

Create custom helper for this. Try something like this:

public static string MyActionLinkWithQuery<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string linkText,
    RouteValueDictionary query) where TController : Controller
{
    RouteValueDictionary routingValues = ExpressionHelper.GetRouteValuesFromExpression(action);

    foreach(KeyValuePair<string, object> kvp in query)
        routingValues.Add(kvp.Key, kvp.Value);

    return helper.RouteLink(linkText, routingValues, null);
}
eu-ge-ne
+1  A: 
<%=Html.ActionLink(LinkName, Action, Controller, new { param1 = value1, param2 = value2 }, new { })%>
Fabian Martin
A: 

You don't need to create extension methods, just change your routing configuration:

  routes.MapRoute(null,
       "MyController/MyAction/{id}",
        new { controller = "MyController", action = "MyAction", id="" } // Defaults
       );


        routes.MapRoute(
       null
      , // Name
       "{controller}/{action}", // URL
       new { controller = "MyController", action = "MyAction" } // Defaults
       );
Lee Smith