I have a ToDictionary extension method, that converts objects into the query string and you can pass it via RouteValues, I pass Model.SearchCriteria, which is a complex object in the folllowing example:
<a href='@Url.Action("ExportAll",
new RouteValueDictionary(Model.SearchCriteria))'>Export All</a>
ToDictionary is an extension method:
public static class ToDictionaryExtensionMethod
{
public static IDictionary<string, object> ToDictionary(this object source)
{
return source.ToDictionary<object>();
}
}
Unfortunately the following code doesn't work:
@Html.ActionLink("Export All", "ExportAll",
new RouteValueDictionary(Model.SearchCriteria.ToDictionary()),
new { @class = "btn btn-default" })
This is because this version of ActionLink accepts routevalues as an object, not as a RouteValueDictionary (in MVC 5). So to make it work, I have to convert the Html attributes to a dictionary as well which uses the correct overload of Html.ActionLink:
@Html.ActionLink("Export All", "ExportAll",
new RouteValueDictionary(Model.SearchCriteria.ToDictionary()),
new Dictionary<string,object>{{"class","btn btn-default"}})