如何使用 IF 条件在 ClientTemplate 中添加超链接 - Ajax Bound Kendo UI for ASP.NET MVC

How to add hyperlink inside ClientTemplate with IF condition- Ajax Bound Kendo UI for ASP.NET MVC

我正在使用 ASP.NET MVC。我想在 Ajax 绑定 Kendo UI 网格中将 Account_Number 列设为 HTML link,当它未被 [= 搜索时23=] 来自上一页。当单击 Account_Number link 时,它应该将 Account_Number 传递给客户控制器方法 'QuickCheckSearch'。
下面的代码不会填充视图中的网格,也不会 call/pass 控制器的值。有人可以更正以下代码吗?

提前致谢。

  columns.Bound(p => p.Account_Number)
 .ClientTemplate("<#if (item.Account_Number == Model.AccountNumber){>Account_Number <# }" + "else{#><a href='" + Url.Action("QuickCheckSearch", "Customer") + "?Account_Number=#=Account_Number#'>#= Account_Number #</a> <# } #>")
 .Title("Account Number");
  1. 阅读http://docs.telerik.com/kendo-ui/framework/templates/overview
  2. 到处都是尖括号。您应该只将它们放在模板的实际 HTML 标记中, 而不是 作为模板语法本身的一部分。尝试:

    .ClientTemplate(
        "# if (Account_Number ==" + @Model.AccountNumber + "){ #" +
            "#= Account_Number #" +
        "# }" +
        "else { #" +
             "<a href = '" + Url.Action("QuickCheckSearch", "Customer") + "?Account_Number=#=Account_Number#'>#= Account_Number#</a>" +
        "# } #"
    )
    

我发现在单独的行中格式化它真的很有帮助,就像您编写实际代码而不是在一行中一样,这样您就可以直观地看到结构。

你也可以这样做,这样会更干净:

.ClientTemplate("#= accountLinkTemplate(data) #")
....
<script>
     function accountLinkTemplate(data) {
        var template = data.Account_Number;
        if (data.Account_Number == " + @Model.AccountNumber + ") {
            template = "<a href = '" + "@Url.Action("QuickCheckSearch", "Customer")" + "?Account_Number=" + data.Account_Number+ "'>" + data.Account_Number+ "</a>";
        }

        return template;
    }
</script>