2016-02-10 36 views
3

我有一個代碼塊如下:如何觸發由Html.Actionlink在ASP.NET MVC的window.open 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窗口「屏幕的站點中心」。我沒有最後一個我所有的需要。

我該如何解決這個問題?

+0

當您調用函數時,您不提供'url','width'或'height'屬性。現在我已經格式化了你的問題,你也可以看到你在ActionLink –

+0

中有一個語法錯誤對不起。我的錯。我確切的代碼是這樣的:@ Html.ActionLink( 「更新」, 「#」, 新{ 的SerialNumber = drEquipment [ 「SERIAL_NUMBER」], StockSpecCd = drEquipment [ 「STOCK_SPEC_CD」], ResourceSpecTypeCd = drEquipment [」 ('/帳戶/設備編輯','350','250','0','0','0','0',' )「 })我試圖使用Html.Actionlink,但我也想設置新的彈出到屏幕中心。 – tdog

+0

你可以使用'edit'按鈕來更新問題中的代碼。 –

回答

0

使用當前代碼,您沒有將3個參數(url,widthheight)傳遞給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); 
    }); 

}); 
+0

我在foreach語句中有我的Html.ActionLink代碼塊。所以我無法給出一個明確的ID。除此之外,我想知道我是否使用preventDefault,我可以傳遞我在ActionLink中指定的參數嗎?我只想讓我的參數傳遞給控制器​​,並在屏幕中心打開新窗口。 – tdog

+0

然後給一個css類而不是Id,並使用css類來綁定click事件。 – Shyju

1

我做了什麼,我想以不同的方式:

@{ 
     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」這樣的屬性。