Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QueryString Parameters in Mvc strongly typed actionlinks

Tags:

asp.net-mvc

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.

like image 685
ZafarYousafi Avatar asked Jan 23 '26 21:01

ZafarYousafi


2 Answers

<%=Html.ActionLink(LinkName, Action, Controller, new { param1 = value1, param2 = value2 }, new { })%>
like image 189
Fabian Martin Avatar answered Jan 25 '26 10:01

Fabian Martin


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);
}
like image 31
eu-ge-ne Avatar answered Jan 25 '26 11:01

eu-ge-ne