kendo grid column with conditionally chosen action

2019-07-03 23:12发布

问题:

Here is what I got:

columns.Bound(t => t.Id)
       .Title("")
       .Template(@<text></text>)
       .ClientTemplate("<a class=\"k-button\" href='" 
            + Url.Action("Edit", "Controller") + "/#=Id#'>Edit</a>")
       .Width(110);

What I need is to pick a specific controller action depending on type of object bound. (different form for e.g. CarEdit when lets say Type==1 and PlaneEdit when Type==2)

I've done similar thing using JS recently (to produce ClientTemplate content) but would greatly appreciate solution without nasty JS.

回答1:

As for now this is my best solution:

columns.Bound(t => t.Id)
       .Title("")
       .Template(@<text></text>)
       .ClientTemplate("#= GetEditTemplate(data)#")
       .Width(110);

function GetEditTemplate(data) {
    var html;

    if (data.Type === 1) {
        html = kendo.format("<a class=\"k-button\" href='" + '@Url.Action("Edit1", "Controller")' + "/{0}" + " '>Edit</a>  ",
                            data.Id
        );
    }
    else {
        html = kendo.format("<a class=\"k-button\" href='" + '@Url.Action("Edit2", "Controller")' + "/{0}" + " '>Edit</a>  ",
                            data.Id
        );
    }

    return html;
}