【问题标题】:ActionLink Confirm not passing parametersActionLink 确认不传递参数
【发布时间】:2018-09-13 21:41:07
【问题描述】:

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

我的代码引用了这个链接here

这是我的代码:

 @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


    【解决方案1】:

    与其像那样手动构建路由值,不如使用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 传递给here

    【讨论】:

    • 感谢您的提示。不幸的是,它没有用。它仍然没有传递参数。 Request.QueryString.Keys 返回一个对象,所以我将“var key”替换为“string key”。
    • 它似乎在传递值,但格式错误。见编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多