连接控制器到淘汰赛

时间:2013-08-23 02:55:47

标签: c# javascript jquery asp.net-mvc knockout.js

我有一个网格,其中我显示列,其中一列有一个图标,一旦点击它应该根据点击的项目的ID下载文件。

因为我使用knockout和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
            });

1 个答案:

答案 0 :(得分: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");
},