英文:
"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(@<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
得分: 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;
}
<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论