简体   繁体   English

Javascript 中的 C# if 语句,Razor/MVC3

[英]C# if statement within Javascript, Razor/MVC3

Ok, so I'm trying to use an "if" statement within my javascript.好的,所以我正在尝试在我的 javascript 中使用“if”语句。 Depending on a boolean in my model, a function should return some html or an empty string.根据我模型中的布尔值,函数应该返回一些 html 或空字符串。 This is basically what I want to do:这基本上就是我想要做的:

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).类似的代码在使用 foreach 循环时非常有效(在上面的示例中基本上用 foreach 替换 if )。 With the if statement I get the error "Encountered end tag "text" with no matching start tag. Are your start/end tags properly balanced?"使用 if 语句,我收到错误“遇到没有匹配的开始标签的结束标签“文本”。您的开始/结束标签是否正确平衡? . . When I remove the <text> tags I get the error "Too many characters in character literal" .当我删除<text>标签时,我收到错误"Too many characters in character literal"

Could anyone point me in the right direction?有人能指出我正确的方向吗?

Thank you!谢谢! :) :)

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 {} . 我添加了一组额外的{}

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. 最终我找到了解决方案,问题是在关闭html标签中未转义为“/”。 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. 我想它可以作为如何在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标签是如何转义的......

try to remove the <text> tags or put them inside the myHtml += ''; 尝试删除<text>标签或将它们放在myHtml += ''; statement 声明

This works too. 这也有效。

function getSomeHtml() {
    var myHtml = '';
    if('@Model.UseSomeNiceHtml' === '@true')
    {
         myHtml += '<div> <p class="label">Whatever</p></div>'; 
    }
    return myHtml;
}
function  @(treeviewConfig.AddNewTreeviewNode)(treeNode) {
        @if (!string.IsNullOrEmpty(NodeIDKey) && !string.IsNullOrEmpty(treeviewConfig.ParentIdExpr)) {
            <text>
                treeNode['@(treeviewConfig.ParentIdExpr)'] = treeNode['@(NodeIDKey)'];
                treeNode['@(NodeIDKey)'] = 0;
        </text>
        }
        @if ( !string.IsNullOrEmpty(treeviewConfig.DisplayExpr)) {
               <text>
            treeNode['@(treeviewConfig.DisplayExpr)'] =  'nue';
         </text>
        }

        @if ( !string.IsNullOrEmpty(treeviewConfig.EditTreeviewNode)) {
             <text>
             treeNode['@(treeviewConfig.EditTreeviewNode)'] = 'nue';
         </text>

        }

        PopulateTreeViewNodeInputs(treeNode);
       @(treeviewConfig.ShowTreeEditPopup)();
    }

This worked for me quite well这对我很有效

Try this: 试试这个:

  function getSomeHtml() {
    @{
      var myHtml = "";
      if(Model.UseSomeNiceHtml)
      {
        myHtml += "<div> <p class='label'>Whatever</p></div>";
      }
    }
    return "@myHtml";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM