asp.net - 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:
我需要将其解析为正确的格式。当没有 html 属性时,它会自动执行此操作。
我也试过这个:
<a href="@Url.Action("Overview", "KnowledgeTransferController", routeDic, "http", string.Empty)" onclick="SaveConfirmation()">Overview</a>
解决方案
而不是像那样手动构建路由值,只需使用一个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
提供的方式传递它。
推荐阅读
- python - 返回在不同列表中匹配的字符串的函数
- javascript - 在访问托管在 AWS 端口 5000 上的后端时面临问题
- java - 无法获取 JasperReports 报告的类“java.awt.Image”的字段“logo”的值
- vba - 在 Outlook 中发送任务通知?
- python - 在切换帧程序中使用 StringVar 挂起
- javascript - 根据嵌套数组中的特定元素匹配人员
- asp.net-core - 如何配置 webpack 以支持某些主机中的虚拟路径?
- dynamics-365 - Dynamics CRM v9.0 getIsDirty 不提供刷新值
- java - jmxagent 在骆驼上下文中添加时抛出 saxparse 异常
- mongodb - Mongo 查询在生产上需要很长时间,但在开发上很快