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

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

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

问题

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

以下是用于Kendo UI ASP.NET MVC的代码:

  1. @{
  2. var queryLevel = ViewBag.queryLevel as string;
  3. }
  4. columns.Template(@<text></text>).ClientTemplate(
  5. "<div class=\"text-center\">" +
  6. "if (" + queryLevel + " == \"Well\") {" +
  7. "<span>Ya</span>" +
  8. "} " +
  9. "<form action=\"/AssetOperationProduction/Update\" method=\"post\" style=\"display:inline\">" +
  10. "<input type=\"hidden\" name=\"Id\" value=\"#= Id #\" />" +
  11. "<button class=\"btn btn-secondary text-white m-1\" type=\"submit\"><i class=\"fa-solid fa-pencil\"></i></button></form>" +
  12. "<form action=\"/AssetOperationProduction/Delete\" method=\"post\" style=\"display:inline\">" +
  13. "<input type=\"hidden\" name=\"Id\" value=\"#= Id #\" />" +
  14. "<button class=\"btn btn-danger text-white m-1\" type=\"submit\"><i class=\"fa-solid fa-trash\"></i> " + queryLevel + " </button></form>" +
  15. "</div>"
  16. ).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

  1. @{
  2. var queryLevel = ViewBag.queryLevel as string;
  3. }
  4. columns.Template(@&lt;text&gt;&lt;/text&gt;).ClientTemplate(
  5. &quot;&lt;div class=\&quot;text-center\&quot;&gt;&quot; +
  6. &quot;if (&quot;+ queryLevel +&quot; == \&quot;Well\&quot;) {&quot; +
  7. &quot;&lt;span&gt;Ya&lt;/span&gt;&quot; +
  8. &quot;}&quot; +
  9. &quot;&lt;form action=\&quot;/AssetOperationProduction/Update\&quot; method=\&quot;post\&quot; style=\&quot;display:inline\&quot;&gt;&quot; +
  10. &quot;&lt;input type=\&quot;hidden\&quot; name=\&quot;Id\&quot; value=\&quot;#= Id #\&quot; /&gt;&quot; +
  11. &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; +
  12. &quot;&lt;form action=\&quot;/AssetOperationProduction/Delete\&quot; method=\&quot;post\&quot; style=\&quot;display:inline\&quot;&gt;&quot; +
  13. &quot;&lt;input type=\&quot;hidden\&quot; name=\&quot;Id\&quot; value=\&quot;#= Id #\&quot; /&gt;&quot; +
  14. &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; +
  15. &quot;&lt;/div&gt;&quot;
  16. ).Width(200);

答案1

得分: 2

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

  1. @{
  2. var queryLevel = ViewBag.queryLevel as string;
  3. }
  4. <script>
  5. var myVariable = '@ViewBag.queryLevel';
  6. </script>

然后可以使用以下方式:

  1. .ClientTemplate(
  2. "<div class=\"text-center\">" +
  3. "#if (myVariable == \"Well\") {#" +
  4. "<span>Ya</span>" +
  5. "#}#"
  6. ...
  7. )

这里有一个示例

英文:

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:

  1. @{
  2. var queryLevel = ViewBag.queryLevel as string;
  3. }
  4. &lt;script&gt;
  5. var myVariable = &#39;@ViewBag.queryLevel&#39;;
  6. &lt;/script&gt;

You can then have:

  1. .ClientTemplate(
  2. &quot;&lt;div class=\&quot;text-center\&quot;&gt;&quot; +
  3. &quot;#if (myVariable == \&quot;Well\&quot;) {#&quot; +
  4. &quot;&lt;span&gt;Ya&lt;/span&gt;&quot; +
  5. &quot;#}#&quot; +
  6. ...
  7. )

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:

确定