[英]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个参数( url
, width
和height
)传递给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.