“if” 在 Kendo UI ASP.NET MVC 中读取文本

huangapple go评论55阅读模式
英文:

"if" reads text in kendo ui asp.net mvc

问题

我有一行代码用于显示数据。由于某些原因,“if”读取的是文本,而不是程序。

以下是用于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);
英文:

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(@&lt;text&gt;&lt;/text&gt;).ClientTemplate(
   &quot;&lt;div class=\&quot;text-center\&quot;&gt;&quot; +

      &quot;if (&quot;+ queryLevel +&quot; == \&quot;Well\&quot;) {&quot; +
         &quot;&lt;span&gt;Ya&lt;/span&gt;&quot; +
      &quot;}&quot; +

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

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

      &quot;&lt;/div&gt;&quot;
).Width(200);

答案1

得分: 2

你基本上正在尝试在JS中访问一个ViewBag变量。查看此线程以获取有关如何执行此操作的详细信息:链接。此外,您应该使用模板语法并将if语句包装在#符号中,以便将JS代码评估为代码,而不是文本:

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

然后可以使用以下方式:

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

这里有一个示例

英文:

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;
}
&lt;script&gt;
   var myVariable = &#39;@ViewBag.queryLevel&#39;;
&lt;/script&gt;

You can then have:

.ClientTemplate(
&quot;&lt;div class=\&quot;text-center\&quot;&gt;&quot; +
  &quot;#if (myVariable == \&quot;Well\&quot;) {#&quot; +
     &quot;&lt;span&gt;Ya&lt;/span&gt;&quot; +
  &quot;#}#&quot; +
 ...
 )

Here is an example.

huangapple
  • 本文由 发表于 2023年5月29日 12:43:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定