3
votes

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.

3

3 Answers

4
votes
<%=Html.ActionLink(LinkName, Action, Controller, new { param1 = value1, param2 = value2 }, new { })%>
1
votes

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);
}
0
votes

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
       );