Switch Statement in Kendo Template

2019-06-17 04:50发布

问题:

Im using Kendo Template and I was able to create a conditional statement (IF statement) as stated on the documentaion. Here's my code.

#if ((item.ControlType) == "tbx"){#
   @(Html.Kendo().AutoComplete()
   .Name("#=ctrlid#")
   .ToClientTemplate())
#}#

This works fine.

But what I'd like to have is a Switch Case Statement. I've tried this but it doesn't work. It says "Invalid Template".

# switch (item.ControlType) {#
  # case "tbx": #
     @(Html.Kendo().AutoComplete()
       .Name("#=ctrlid#")
       .ToClientTemplate())
  # break; #
  # }#

Am I missing something? Or is it just impossible for Kendo Template to interpret Switch Case Statements? (about the latter, I dont think so..I'm definitely missing something)

Can anyone help me please? Thanks!^^

回答1:

I check your case and I found the reason for the error. Basically to keep the JavaScript valid you will have to use only one pair of # symbols surounding the switch statement and the first case statement.

e.g.

instead of:

# switch (item.ControlType) {#
# case "tbx": #

use:

# switch (item.ControlType) {
  case "tbx": #

To demonstrate it , here is a jsbin. Basically there should not be problems if you combine the switch with some widget generated by the MVC wrappers. If there is, let me know.