2013-08-23 29 views
0

我有一個網格,其中我顯示列和其中一列有一個圖標,一旦點擊它應該下載一個文件的基礎上點擊項目的ID。連接控制器,以淘汰js

因爲我使用基諾和jQuery的JavaScript來顯示網格與圖標一起。我怎樣才能將文件的方法連接到我的js文件中的圖標?

JS文件:

onDataClick: function (row) { 
//Call the method from controller to allow downloading file 
       }, 

控制器 - 獲得方法:

public FileResult GetFile(int id) 
     { 
      ..... 
     } 

更新

查看:

@{ 
    ViewBag.Title = "Some Title"; 
    string url = Url.Action("GetFile"); 
} 

<div data-bind="template: { name: 'knockoutGridTemplate', data: grid }" class="gridWrapper"></div> 

在網格中的列我在js文件之一:

builtColumns.push({ 
       property: 'hasStuff', 
       header: 'File Download', 
       dataCss: 'iconHolder', 
       onDataClick: function (row) { 


       }, 
       dataFormatter: function (row) { 
        if (row[this.property]) return ' '; 
        return ''; 
       }, 
       dataLinkCss: 'icon-file', 
       grouping: 3 
      }); 
+0

看起來你也許可以用一個iframe和一個自定義KnockoutJS結合在詳細的做到這一點[通過在ASP.NET MVC中發佈JSON數據下載CSV文件]的接受答案(http://stackoverflow.com/questions/18114322/download-csv-file-by-posting-json-data-in-asp-net- MVC)。 –

回答

1

您可以在您的視圖的東西不喜歡

@{ 
    string getFileUrl = Url.Action("GetFile"); 
} 

/* in your viewModel, depend how you are doing it, you can do inside your item */ item.getFileUrl = '@getFileUrl' + ?id= this.id;

和在你的html中:

<div data-bind="foreach: item"> 
    <a data-bind="attr : { href = getFileUrl}">get file</a> 
</div> 

* 注:無需觀測*

編輯:

onDataClick: function (row) { 
    //Call the method from controller to allow downloading file 
    window.open('@getFileUrl' + '?id=' + row.id, "_blank"); 
}, 
+0

,因爲我沒有一個直接查看html頁面 - 網格正在生成並顯示在js中 - 我可以將它添加到js中的onclick嗎? – Masriyah

+0

@Masriyah是的,我編輯了我的答案來反映這一點。 –

+0

我想我真的很接近 - 我更新了我的代碼,如果你可以檢查並確保我有我應該的東西。從視圖頁面我將傳遞給控制器​​的ID。感謝您的幫助 – Masriyah