0

I have a column line of code to display the data. For some reason, the "if" reads text, not a program

the following line code for kendo ui asp.net mvc

@{
  var queryLevel = ViewBag.queryLevel as string;
}


columns.Template(@<text></text>).ClientTemplate(
   "<div class=\"text-center\">" +

      "if ("+ queryLevel +" == \"Well\") {" +
         "<span>Ya</span>" +
      "}" +

      "<form action=\"/AssetOperationProduction/Update\" method=\"post\" style=\"display:inline\">" +
      "<input type=\"hidden\" name=\"Id\" value=\"#= Id #\" />" +
      "<button class=\"btn btn-secondary text-white m-1\" type=\"submit\"><i class=\"fa-solid fa-pencil\"></i></button></form>" +

      "<form action=\"/AssetOperationProduction/Delete\" method=\"post\" style=\"display:inline\">" +
      "<input type=\"hidden\" name=\"Id\" value=\"#= Id #\" />" +
      "<button class=\"btn btn-danger text-white m-1\" type=\"submit\"><i class=\"fa-solid fa-trash\"></i> "+ queryLevel +" </button></form>" +

      "</div>"
).Width(200);

1 Answers1

2

You are essentially trying to access a ViewBag variable in JS. Check this thread for details on doing so. Also you should use the template syntax and wrap the if statement in # symbols so the JS code is evaluated as such, rather than as text:

@{
   var queryLevel = ViewBag.queryLevel as string;
}
<script>
   var myVariable = '@ViewBag.queryLevel';
</script>

You can then have:

.ClientTemplate(
"<div class=\"text-center\">" +
  "#if (myVariable == \"Well\") {#" +
     "<span>Ya</span>" +
  "#}#" +
 ...
 )

Here is an example.

Aleksandar
  • 1,029
  • 5
  • 6