1

I am trying to add a confirmation dialog to an action link. I have a parameter in the URL called "interviewTypeID" that needs to be passed to the controller.

I referenced this link here for my code:

This is my code:

 @Html.ActionLink("Instructions", "Index", "KnowledgeTransferController", new { interviewTypeID = Request.QueryString["interviewTypeID"], employeeNumber = Request.QueryString["employeeNumber"], ID = Request.QueryString["ID"] }, new {onclick="return confirm('Are you sure?');"})

However whenever I add the onclick in the HtmlAttribute parameter, the link breaks because it does not pass the interviewTypeID parameter set by set by Request.QueryString. At first I thought the Request.QueryString is broken by the javascript. But even when I write

, new {}

it still breaks, without any javascript added.

How do I create an alert for this ActionLink while still passing parameters from the URL?

Thank you for your help.

Edit:

It isn't that it isn't passing variables. It redirects to the following invalid URL:

https://localhost/DCAS.ExitInterview.Web_1_3_0/KnowledgeTransferController/Overview?Count=5&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D

I need to parse it into the correct format. It does this automatically when there is no html attribute.

I also tried this:

<a href="@Url.Action("Overview", "KnowledgeTransferController", routeDic, "http", string.Empty)" onclick="SaveConfirmation()">Overview</a>
Steve Scott
  • 1,441
  • 3
  • 20
  • 30

1 Answers1

1

Rather than build route values manually like that, just use a RouteValueDictionary collection to hold query string data and pass to ActionLink helper like this:

@{
   var routeDic = new RouteValueDictionary(ViewContext.RouteData.Values);
   foreach (string key in Request.QueryString.Keys)
   {
       routeDic[key] = Request.QueryString[key].ToString();
   }
}

@* Usage *@
@Html.ActionLink("Instructions", "Index", "KnowledgeTransferController", routeDic, new { onclick = "return confirm('Are you sure?');" })

As an alternative, you may try create an IEnumerable property and assign it with query string values, then pass it as RouteValueDictionary as provided here.

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • Thanks for the tip. Unfortunately it did not work. It is still not passing the parameters. Request.QueryString.Keys returns an object, so I replaced "var key" with "string key". – Steve Scott Sep 14 '18 at 15:41
  • It seems like it was passing values but in the wrong format. See edit. – Steve Scott Sep 20 '18 at 19:19