Reputation: 9298
I'm trying to programatically generate a link by routename and routevalues. I store the routes and values in the database.
How can I adjust this helper in order to make it work?
public static string GenerateLink(this HtmlHelper helper, string routeName, Dictionary<string, string> parameters)
{
// ??
RouteValueDictionary rd = new RouteValueDictionary();
foreach (var item in parameters)
{
rd.Add(item.Key, item.Value);
}
// string url = ??
return url;
}
to use it like:
<%= Html.GenerateLink(Model.SomeLinkName, Model.RouteName, ..?) %>
/M
Upvotes: 0
Views: 942
Reputation: 3043
Actually this method already exists for you.
Html.RouteLink(string LinkText, string routeName, RoutevalueDictionary routeValues)
The only thing you need to do is turn your IDictionary into a RouteValueDictionary which again is quite simple as the constructor for a RVD can take an IDictionary which saves you from doing the foreach loop in your example.
So finally all you need is
Html.RouteLink(string LinkText, string routeName, new RoutevalueDictionary(parameters) )
Upvotes: 1