繁体   English   中英

如何在ASP.NET MVC 4中通过Html.Actionlink触发window.open

[英]How to trigger window.open by Html.Actionlink in ASP.NET MVC 4

我有一个代码块如下:

@foreach (System.Data.DataRow drEquipment in Model.EquipmentList.Rows)
    {
         <tr>
             <td valign="top">@drEquipment["ColumnName"]<br /></td>
             <td valign="top">@drEquipment["ColumnName"] - @drEquipment["ColumnName"]<br /></td>
             <td valign="top">@drEquipment["ColumnName"]</td>
             <td valign="top">@drEquipment["ColumnName"] - @drEquipment["ColumnName"]</td>
             <td>@Html.ActionLink("Güncelle", "UpdateAction", new { SerialNumber = drEquipment["ColumnName"], StockSpecCd = drEquipment["ColumnName"], ResourceSpecTypeCd = drEquipment["ColumnName"] }, new { popup="{\"height\":250, \"width\":350}" })</td>  
         </tr>
    }
@Html.ActionLink("Update", "UpdateAction", new { SerialNumber = drEquipment["ColumnName"], StockSpecCd = drEquipment["ColumnName"], ResourceSpecTypeCd = drEquipment["ColumnName"], new { popup="{\"height\":250, \"width\":350}" })

它运行良好,但我不能站点弹出屏幕中心。 这就是为什么我有一个onclick事件如下:

onclick="MyPopup('/Account/UpdateAction', '350', '250')"

所以我不能使用像id这样的变量来使用e.preventDefault,因为有一个foreach语句。

我在按钮点击事件上解决了这个问题并且效果很好:

<button onclick="MyPopup('/Account/UpdateAction', '350', '250')">Update</button>

function MyPopup(url, width, height) {
var leftPosition, topPosition;
//Allow for borders.
leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
//Allow for title and status bars.
topPosition = (window.screen.height / 2) - ((height / 2) + 50);
//Open the window.
window.open(url, "_blank",
"status=no,height=" + height + ",width=" + width + ",resizable=yes,left="
+ leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY="
+ topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");

}

但我无法使用Html.Actionlink的功能。 我把一个onclick事件作为htmlAttritube,但它不起作用。

我真正需要的是将我的参数传递给控制器​​并打开新的_blank窗口“屏幕的站点中心”。 没有最后一个,我有我所有的需求。

我该如何解决这个问题?

我以不同的方式做了我想做的事:

 @{
      long WarehouseId = string.IsNullOrWhiteSpace(drEquipment["WAREHOUSE_ID"].ToString()) ? 0 : Convert.ToInt64(drEquipment["WAREHOUSE_ID"]);
      string link = "/Account/EquipmentEdit?SerialNumber=" + drEquipment["SERIAL_NUMBER"] + "&StockCode=" + drEquipment["STOCK_CODE"] + "&WarehouseId=" + WarehouseId + "&WarehouseTypeCode=" + drEquipment["WAREHOUSE_TYPE_CODE"].ToString() + "&WhsStatusType=" + drEquipment["WHS_STATUS_TYPE"].ToString();
  }

  <td><a href="#" onclick="MyPopup('@link', '350', '250');">Update</a></td>

function MyPopup(url, width, height) {
    var leftPosition, topPosition;
    //Allow for borders.
    leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
    //Allow for title and status bars.
    topPosition = (window.screen.height / 2) - ((height / 2) + 50);
    //Open the window.
    window.open(url, "_blank",
    "status=no,height=" + height + ",width=" + width + ",resizable=yes,left="
    + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY="
    + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
}

如果有办法处理Html.ActionLink,我想知道。 我不想在我的cshtml页面中看到像“a href”这样的属性。

使用当前代码,您不会将3个参数( urlwidthheight )传递给MyPopup()方法!

你需要获取并传递目标网址,宽度和高度到你的MyPopup方法,一种方法是,给链接一个id并使用unobutrisuve javascript来处理click事件。 您可以将高度和宽度保持为链接的html 5数据属性。

@Html.ActionLink("Update", "EquipmentEdit", 
               new { SerialNumber = drEquipment["SERIAL_NUMBER"],
                     StockSpecCd = drEquipment["STOCK_SPEC_CD"],
                     ResourceSpecTypeCd = drEquipment["RESOURCE_SPEC_TYPE_CD"] },
                     new { id="updateLink", data_height="300", data_width="300" } )

在您的javascript(使用jQuery)中,收听此链接的click事件

$(function(){

  $("#updateLink").click(function(e){
     e.preventDefault();

     var url=$(this).attr("href");
     var height=$(this).data("height");
     var width=$(this).data("width");

     alert(height);
     // execute your custom code for window.open here
     // call MyPopup(url,width,height);
  });

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM