【问题标题】:connecting controller to knockout js将控制器连接到淘汰赛js
【发布时间】:2013-08-23 02:55:47
【问题描述】:

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

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

【问题讨论】:

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


【解决方案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>

*注意:不需要 observables *

编辑:

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

【讨论】:

  • 因为我没有直接查看 html 页面 - 网格正在生成并在 js 中显示 - 我可以将它添加到 js 中的 onclick 吗?
  • 我想我真的很接近 - 如果你可以检查并确保我有我应该做的事情,我会更新我上面的代码。在视图页面中,我将为控制器传递一个 id。感谢您的帮助
  • 看起来不错,我认为为每一行创建一个函数并不是一个好主意。我会说最好有一个通用函数接收一个 id 并在行内调用但不在每一行上声明。
猜你喜欢
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 2018-08-13
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
相关资源
最近更新 更多