【问题标题】:Html.ActionLink Javascript in hrefHtml.ActionLink Javascript 中的 href
【发布时间】:2017-02-17 17:24:05
【问题描述】:

我正在尝试通过以下方式通过辅助方法生成 ActionLink:

return helper.ActionLink(linkText, actionName, controllerName, route, attributes);

我传入的值如下:

new ActionLinkModel
{
    LinkText = "Help",
    ElementId = "HelpLink",
    ActionName = "javascript:void(0);",
    ControllerName = "",
    HtmlAttributes = new {onclick = "RunSomeFunction(this, 'Something');"}
}

基本上模型会映射到您在我的第一行代码中看到的值。

我在 HTML 页面上得到的输出是这样的:

<a href="/MyWebsite/MyController/javascript%3avoid(0)%3b" id="HelpLink" onclick="RunSomeFunction(this, 'Something');">Help</a>

我希望输出是这样的:

<a href="javascript:void(0);" id="HelpLink" onclick="RunSomeFunction(this, 'Something');">Help</a>

我将如何做到这一点?使用 ActionLink 辅助方法的想法,因为它允许我遍历菜单并将其打印出来,并让菜单成为后端的控制器。

【问题讨论】:

    标签: c# asp.net asp.net-mvc razor asp.net-mvc-5


    【解决方案1】:

    您可以尝试在属性中手动添加href

    new ActionLinkModel
    {
        LinkText = "Help",
        ElementId = "HelpLink",
        ActionName = "",
        ControllerName = "",
        HtmlAttributes = new {@href="javascript:void(0);",onclick = "RunSomeFunction(this, 'Something');"}
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-20
      • 2014-07-30
      • 2011-08-15
      • 2015-06-16
      • 2012-02-23
      • 2019-02-28
      • 2014-06-13
      • 2011-04-09
      • 1970-01-01
      相关资源
      最近更新 更多