简体   繁体   English

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

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

I have a code block as follows: 我有一个代码块如下:

@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}" })

It is working well like this but I couldn't site popup center of the screen. 它运行良好,但我不能站点弹出屏幕中心。 That's why I have an onclick event as follows: 这就是为什么我有一个onclick事件如下:

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

So I couldn't use a variable like id to use e.preventDefault because there is a foreach statement. 所以我不能使用像id这样的变量来使用e.preventDefault,因为有一个foreach语句。

I solve this problem on button click event and it works well: 我在按钮点击事件上解决了这个问题并且效果很好:

<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");

} }

But I couldn't use my function with Html.Actionlink. 但我无法使用Html.Actionlink的功能。 I put an onclick event as an htmlAttritube but it didn't work. 我把一个onclick事件作为htmlAttritube,但它不起作用。

What I exactly need is pass my parameters to controller and open new _blank window "site center of the screen". 我真正需要的是将我的参数传递给控制器​​并打开新的_blank窗口“屏幕的站点中心”。 I have all my needs without last one. 没有最后一个,我有我所有的需求。

How do I fix this problem? 我该如何解决这个问题?

I did what I want in a different way: 我以不同的方式做了我想做的事:

 @{
      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");
}

If there is a way to do with Html.ActionLink, I would like to know it. 如果有办法处理Html.ActionLink,我想知道。 I don't want to see attributes like "a href" in my cshtml page. 我不想在我的cshtml页面中看到像“a href”这样的属性。

With the current code you are not passing the 3 parameters ( url , width and height ) to the MyPopup() method ! 使用当前代码,您不会将3个参数( urlwidthheight )传递给MyPopup()方法!

You need to get and pass the target url, width and height to your MyPopup method, One way to do that is, give an id to the link and use unobutrisuve javascript to handle the click event. 你需要获取并传递目标网址,宽度和高度到你的MyPopup方法,一种方法是,给链接一个id并使用unobutrisuve javascript来处理click事件。 You can keep the height and width as html 5 data attributes to the link. 您可以将高度和宽度保持为链接的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" } )

And in your javascript(using jQuery), listen to the click event of this link 在您的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.

相关问题 使用@ Html.ActionLink发布textarea-ASP.NET MVC 5 - Post textarea using @Html.ActionLink - ASP.NET MVC 5 在ASP.Net MVC中,如何在单击时显示和隐藏3个@ Html.ActionLink按钮? - Within ASP.Net MVC, how do I show and hide 3 @Html.ActionLink buttons on click? 如何在asp.net mvc中的html.actionlink中调用javascript确认对话框功能? - how to call javascript confirm dialog function in html.actionlink in asp.net mvc? 如何在asp.net mvc 的html.actionlink 中调用javascript 函数? - how to call javascript function in html.actionlink in asp.net mvc? c#asp.net mvc-使用参数从html.actionlink调用脚本函数 - c# asp.net mvc - calling script function from html.actionlink with a parameter ASP.net MVC window.open URL不起作用 - ASP.net MVC window.open URL not working 为什么在加载视图和单击ASP.NET MVC中的Html.ActionLink之间,我的routeValues变量会发生变化? - Why is my routeValues variable changing between my view loading and me clicking on my Html.ActionLink in ASP.NET MVC? asp .net @ Html.ActionLink动态ID(参数) - asp .net @Html.ActionLink dynamic id (parameter) 如何在window.open函数ASP.NET中调用WebConfig appsetting值? - How WebConfig appsetting value can be called inside a window.open function ASP.NET? ASP.NET-Window.Open(URL)-缓存了文件,如何停止它? - ASP.NET - Window.Open(URL) - File is cached, how do I stop it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM