Javascript 中的 C# if 语句,Razor/MVC3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9681089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# if statement within Javascript, Razor/MVC3
提问by Erik Kinding
Ok, so I'm trying to use an "if" statement within my javascript. Depending on a boolean in my model, a function should return some html or an empty string. This is basically what I want to do:
好的,所以我正在尝试在我的 javascript 中使用“if”语句。根据我模型中的布尔值,函数应该返回一些 html 或空字符串。这基本上就是我想要做的:
function getSomeHtml() {
var myHtml = '';
@if(Model.UseSomeNiceHtml)
{
<text>
myHtml += '<div> <p class="label">Whatever</p></div>';
</text>
}
return myHtml;
}
Similar code works really well when using a foreach loop (basically replacing if with foreach in the example above). With the if statement I get the error "Encountered end tag "text" with no matching start tag. Are your start/end tags properly balanced?". When I remove the <text>tags I get the error "Too many characters in character literal".
类似的代码在使用 foreach 循环时非常有效(在上面的例子中基本上用 foreach 替换 if )。使用 if 语句,我收到错误“遇到没有匹配的开始标签的结束标签“文本”。您的开始/结束标签是否正确平衡?. 当我删除<text>标签时,我收到错误“字符文字中的字符太多”。
Could anyone point me in the right direction?
有人能指出我正确的方向吗?
Thank you! :)
谢谢!:)
采纳答案by Erik Kinding
Ok, first: thanks for your input, it got me thinking. Eventually I found the solution and the problem was an unescaped "/" in a closing html tag. With those tags unescaped, my tags freaked out. Anyway, I figured I'd share with you what my finished code looks like. I guess it can serve as an example of how to use both C# loops and if-statements in a javascript function.
好的,首先:感谢您的投入,这让我开始思考。最终我找到了解决方案,问题是在结束的 html 标签中出现了一个未转义的“/”。那些标签没有转义,我的标签吓坏了。无论如何,我想我会与您分享我完成的代码的样子。我想它可以作为如何在 javascript 函数中同时使用 C# 循环和 if 语句的示例。
function getSubActivitiesHtml(participantId) {
var html = "";
@{
if(Model.UseSubActivities)
{
<text>
html += "<div class=\"textinput req\"><div class=\"checkbox req\">";
</text>
foreach (var subActivity in Model.SubActivities)
{
<text>
html += "<p><input id=\"activity_" + participantId + "_@(subActivity.Id)\" name=\"Participants[" + participantId + "].SelectedSubActivities\" value=\"@(subActivity.Id)\" type=\"checkbox\" />";
html += "<label for=\"activity_" + participantId + "_@(subActivity.Id)\">@(subActivity.Name)</label></p>";
</text>
}
<text>
html += "<\/div><p class=\"label\">Delaktiviteter</p><\/div>";
</text>
}
}
return html;
}
Notice how the closing html tags are escaped...
注意关闭的 html 标签是如何转义的...
回答by trembon
try to remove the <text>tags or put them inside the myHtml += '';statement
尝试删除<text>标签或将它们放在myHtml += '';语句中
回答by AndersDaniel
Ok, here's something that works for me. Tested just now.
好的,这里有一些对我有用的东西。刚刚测试过。
function getSomeHtml() {
var myHtml = '';
@{
if (Model.UseSomeNiceHtml)
{
<text>
myHtml += '<div> <p class="label">Whatever</p></div>';
</text>
}
}
return myHtml;
}
I added an extra set of {}.
我添加了一组额外的{}.
回答by Zaid Masud
Try this:
尝试这个:
function getSomeHtml() {
@{
var myHtml = "";
if(Model.UseSomeNiceHtml)
{
myHtml += "<div> <p class='label'>Whatever</p></div>";
}
}
return "@myHtml";
}
回答by RayLoveless
This works too.
这也有效。
function getSomeHtml() {
var myHtml = '';
if('@Model.UseSomeNiceHtml' === '@true')
{
myHtml += '<div> <p class="label">Whatever</p></div>';
}
return myHtml;
}

