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.
<%=Html.ActionLink(LinkName, Action, Controller, new { param1 = value1, param2 = value2 }, new { })%>
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With