views:

23

answers:

1

Hello

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

+1  A: 

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

Chao
How shall I assign the values if I got them in Model.Routeparameters (.ParameterKey and .ParameterValue)?
molgan
If your Model.RouteParameters property is already a RouteValueDictionary you can just pass it to Html.RouteLink: Html.RouteLink("link text", "routeName", Model.RouteParameters )
Hector