首页 > 解决方案 > ActionLink 确认不传递参数

问题描述

我正在尝试向操作链接添加确认对话框。我在 URL 中有一个名为“interviewTypeID”的参数,需要传递给控制器​​。

我在这里为我的代码引用了这个链接:

这是我的代码:

 @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?');"})

但是,每当我在 HtmlAttribute 参数中添加 onclick 时,链接就会中断,因为它没有传递由 Request.QueryString 设置的 interviewTypeID 参数。起初我认为 Request.QueryString 被javascript破坏了。但即使我写

, new {}

它仍然中断,没有添加任何 javascript。

如何在仍从 URL 传递参数的同时为此 ActionLink 创建警报?

谢谢您的帮助。

编辑:

并不是说它没有传递变量。它重定向到以下无效 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

我需要将其解析为正确的格式。当没有 html 属性时,它会自动执行此操作。

我也试过这个:

<a href="@Url.Action("Overview", "KnowledgeTransferController", routeDic, "http", string.Empty)" onclick="SaveConfirmation()">Overview</a>

标签: asp.net

解决方案


而不是像那样手动构建路由值,只需使用一个RouteValueDictionary集合来保存查询字符串数据并像这样传递给ActionLink助手:

@{
   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?');" })

作为替代方案,您可以尝试创建一个IEnumerable属性并为其分配查询字符串值,然后按照此处RouteValueDictionary提供的方式传递它。


推荐阅读