【问题标题】:MVC button should open url in new window/tabMVC 按钮应在新窗口/标签中打开 url
【发布时间】:2016-09-10 00:55:11
【问题描述】:

目前尚不清楚正确的语法应该是什么,我已经尝试了一些示例但没有运气,这是我当前的按钮代码,我需要它在单击时保持为按钮在新窗口中打开操作/新标签

<input type="button" title="Read" value="@T("Account.DownloadableProducts.Read")" onclick="location.href='@Url.Action("GetDownload", "Download", new { orderItemId = Model.DownloadMappingIds[product.Id].guid })'" />

谢谢

【问题讨论】:

    标签: asp.net-mvc button target


    【解决方案1】:

    你可以使用javascript函数来做到这一点

     <input type="button" value="MyButton"  onclick="myfunction(this)" 
     data-myurl="@Url.Action("aa", "bb", new { id=@id})"  /> 
    
    
      <script>
       function myfunction(e)
         {
         var url = $(e).data('myurl');
         var popupWindow = window.open(url,title,'scrollbars=1,height=650,width=1050');
        }
      </script>
    

    【讨论】:

    • 太棒了。对于我们其他不太了解 MVC 的人来说,什么是“aa”和“bb”?你应该有类似 @Url.Action("Read", "MyController", new { id=@id}) 的东西。抱歉,这只是我的一个小问题,人们认为我们马上就知道了,而不是完成示例。
    • 对此的一种变体:这可能会更混乱,但您可以说data-myurl="/OtherBranch/Index?id=@id" 之类的内容,然后使用var newUrl = window.location.href.split('/FirstBranch')[0] + e.attr('data-myurl'); 重定向到myfunction() 内的内容,而不是@Url.Action。跨度>
    【解决方案2】:

    我已经为此苦苦挣扎了一段时间,解决方案是,您应该使用 formtarget="_blank",而不是 target="_blank"。

    谢谢!

    【讨论】:

      【解决方案3】:

      我正在努力阅读您的问题和代码,但要打开新窗口,使用以下代码:

      target = "_blank"
      

      是否可以稍微整理一下您的问题,然后我可以为您提供进一步的帮助。

      【讨论】:

        猜你喜欢
        • 2015-10-28
        • 1970-01-01
        • 1970-01-01
        • 2011-06-06
        • 2017-04-07
        • 2020-12-13
        • 2014-11-05
        • 2011-07-05
        相关资源
        最近更新 更多