0

I'd like to be able to feed in a list of parameter names and values into a Html.Actionlink but the helper doesn't create the parameters as I would like. Any ideas how to do this?

public class ParameterNameValue
{
     public string ParameterName { get; set; }
     public string ParameterValue { get; set; }
 }

View

 @foreach (var action in post.FeedActions)
    {
        var parameters = "";
        foreach (var param in action.Parameters)
        {
           parameters += param.ParameterName + "=" + param.ParameterValue + ",";
         }
         @Html.ActionLink(@action.Label, action.ActionName,
         new { controller = action.Controller, id = action.CommunityId, slug = action.Slug,
         Fromfeed=true,parameters }, new { @class = action.Classes })
   }

yields a link like this: enter image description here

Whereas I need the parameters part to look like:

?FromFeed=true&MatchId=1234&InnerId=5678

edit: I got it working by just manually creating the tag, but no doubt there's a nice way of doing this by creating a custom helper.

 <a href="/@action.Slug/@action.CommunityId/@action.Controller-@action.ActionName?Fromfeed=true&@parameters">@action.Label</a>
BMills
  • 881
  • 1
  • 10
  • 24

1 Answers1

0

I'd suggest you to extend the classic ActionLink helper with a prototype similar to this (add a parameter for your specific class) :

public static MvcHtmlString ActionLinkCustom(this HtmlHelper html, string linkText, string actionName, string controllerName,  object routeValues, List<ParameterNameValue> yourOtherValues)

In the code, check if you got any custom values. If such, add them to the RouteValuesDictionnary. Then use the classic ActionLink helper providing this modified RouteValuesDictionnary.

Note : you can work on the routeValues using this

IDictionary<string, object> RouteValues = HtmlHelper.ObjectToDictionary(routeValues);
Ajirakun
  • 1
  • 2