如何在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}" })
这样工作正常,但我无法在屏幕的弹出窗口中找到站点.这就是为什么我有一个如下的onclick事件:
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="MyPopup('/Account/UpdateAction', '350', '250')"
所以我不能使用id之类的变量来使用e.preventDefault,因为有一个foreach语句.
So I couldn't use a variable like id to use e.preventDefault because there is a foreach statement.
我在按钮单击事件中解决了此问题,并且效果很好:
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");
}
但是我无法在Html.Actionlink中使用我的函数.我将onclick事件作为htmlAttritube,但没有用.
But I couldn't use my function with Html.Actionlink. I put an onclick event as an htmlAttritube but it didn't work.
我真正需要的是将我的参数传递给控制器,并打开新的_blank窗口屏幕的中心".我的所有需求都没有最后一个.
What I exactly need is pass my parameters to controller and open new _blank window "site center of the screen". I have all my needs without last one.
如何解决此问题?
推荐答案
我以不同的方式完成了我想做的事情:
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");
}
如果有与Html.ActionLink有关的方法,我想知道.我不想在我的cshtml页面中看到诸如"a href"之类的属性.
If there is a way to do with Html.ActionLink, I would like to know it. I don't want to see attributes like "a href" in my cshtml page.
这篇关于如何在ASP.NET MVC 4中由Html.Actionlink触发window.open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!