TAGS :Viewed: 10 - Published at: a few seconds ago

[ Kendo Treeview Remote Binding c# variable inside template ]

I'm using MVC Razor view and Kendo Controls

How do I keep a global variable that reside in my C# code block but at the same time, also available inside my Kendo Template so that I can incerement it for every items I fetch?

What I need is this:

Kendo Treeview:

@(Html.Kendo().TreeView()
  .Name("DatasourceTreeview")
  .DataTextField("ControlId")
  .DataSource(dataSource => dataSource
  .Read(read => read
    .Action("RetrieveTreeviewItems", "Customizer")
    .Data("submitParams")).Events(e => e.RequestStart("loadParams")))
   .TemplateId("treeview-template")
  )

Variable:

@ { int globalVariable = 1; }

Template:

<script id="treeview-template" type="text/kendo-ui-template">  
 # switch (item.ControlType) {
     case "control1": #
        globalVariable += 1;
     # break; #
     # case "control2": #
        globalVariable += 2;
     # break; #
     # default: #
        globalVariable += 3;
     # break; #
</script>

Result: incremented globalVariable

After passing through the template and rendering the treeview, I would like to use the incremented globalVariable. But the C# variable itself is not visible inside the Template. How can I;

  • Initialize a variable in C# code block then Pass it to the template
  • Increment depending on the data retrieved
  • Pass it back to C# code block

Any help would be appreciated. Thanks!

Answer 1


You need to create a JavaScript variable because Kendo Templates are nothing else but JavaScript code. Here is how:

<script>
var globalVariable = @globalVariable;
</script>