【问题标题】:Opening a new window using MVC使用 MVC 打开一个新窗口
【发布时间】:2011-04-06 00:54:14
【问题描述】:

基本上我想要一个按钮,可以在包含我的http://fake.com/Report/PageFixes... 页面的新窗口中打开一个页面。目前我在表单中有一个按钮,但如果有更好的方法可以做到这一点,那么我也准备好了。

<% using (Html.BeginForm("PageFixes", "Report", new { id = 9 }, FormMethod.Get, new { onSubmit="window.open()" })) %>
<% { %>
    <%= Html.CSButton(new ButtonViewData() { Text = "Submit" })%>
<% } %>

【问题讨论】:

    标签: c# javascript asp.net asp.net-mvc


    【解决方案1】:

    您不需要form。只需将以下 HTML 添加到您的视图中:

    <button type="button" onclick="window.open('<%= Url.Action("PageFixes", "Report", new { id = "9" } ) %>', '_blank'); return false;">submit</button>
    

    我不确定您是如何获得ID9,但我认为它来自您的模型,因此您可以将"9" 替换为Model.ID 或其他东西。

    关键是使用Url.Action 生成网址,然后只需使用基本的javascript 函数window.open 在新的浏览器窗口中打开网址。

    【讨论】:

    【解决方案2】:

    像这样的按钮上放一些 javascript 怎么样

    <button type="button" onclick="window.open('http://fake.com/Report/PageFixes/9')">submit</button>
    

    或者,如果您希望动态构建 URL

    <button type="button" onclick="window.open('<%= Request.Url.Scheme + "://"+Request.Url.Authority+Request.ApplicationPath %>Report/PageFixes/9')">submit</button>
    

    【讨论】:

      【解决方案3】:

      您仍然可以通过 onclick JavaScript 函数以老式方式执行此操作。我是这样做的,我需要将 ID 从模型传递到我的新 URL,该 URL 是在我的项目的同一目录分支上的文件夹中的另一个视图:

      <input type="button" id="@Model.Id" data-id="@Model.Id" onclick="openUrl(this)" data-myurl="/OtherBranch/Detail?id=@Model.Id" />
      
      function openUrl(e) {
          var id = e.id;
          var obj = $("[id='"+id+"']");
          var currentUrl = window.location.href;
          var newBranch = obj.attr('data-myurl');
          var newUrl = currentUrl.split('/FirstBranch')[0] + newBranch;
          window.open(newUrl, "", "height=600,width=400,addressbar=no,menubar=no"); // pop-up window -- should probably ensure no master page is used
          //window.location.href = newUrl; // <-- total redirect option
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        • 1970-01-01
        • 2012-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多