使用ASP .NET MVC编辑Kendo UI网格时重定向

Léo*_*sne 2 asp.net-mvc kendo-ui kendo-grid kendo-asp.net-mvc

当我使用带有ASP .NET MVC的Kendo UI网格点击"编辑"按钮时,我想向另一个页面添加重定向.

这是基本代码:

@(Html.Kendo().Grid<ViewModel>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(x => x.Id);
        columns.Bound(x => x.Name);
        columns.Bound(x => x.Field1);
        columns.Command(commands =>
        {
            commands.Edit();
            commands.Destroy();
        })
    })
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model => model.Id(x => x.Id))
        .Read(read => read.Action("Read", "Home"))
        .Update(update => update.Action("Edit", "Home"))
        .Destroy(destroy => destroy.Action("Destroy", "Home"))
    )
)
Run Code Online (Sandbox Code Playgroud)

我试图使用HTML属性,但它不起作用:

commands.Edit().HtmlAttributes(new { @class = "edit" });
Run Code Online (Sandbox Code Playgroud)

然后,我尝试添加一个自定义编辑(通过commands.Custom(...)但不幸的是它仅用于.Server()数据绑定.

我可以用客户端模板来做,但我真的想使用Kendo UI提出的默认按钮:

columns.Template(@<text></text>)
            .ClientTemplate(
                "<a href='" + Url.Action("Edit", "Home") + "/#=Id#'>Edit</a>");
Run Code Online (Sandbox Code Playgroud)

你还有其他想法吗?

提前致谢.

Dre*_*yes 6

您应该能够使用自定义命令,即使使用Ajax数据源也是如此.我刚刚使用以下代码在本地测试了它,以确保它仍然有效.

来自视图的代码:

<script type="text/javascript">
    function redirectTest(e) {
        e.preventDefault();

        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        alert(dataItem.Name);
    }
</script>

@(Html.Kendo().Grid<ViewModel>()
.Name("testing")
.Columns(columns => 
    {
      columns.Bound(x => x.Id);
      columns.Bound(x => x.Name);
      columns.Command(command => command.Custom("Edit").Click("redirectTest"));
    })
.DataSource(dataSource => dataSource
    .Ajax()
    .Read(read => read.Action("ReadAction", "ControllerName"))
)
)
Run Code Online (Sandbox Code Playgroud)

来源:自定义命令演示