Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解决这个Javascript/Razor问题?_C#_Jquery_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 如何解决这个Javascript/Razor问题?

C# 如何解决这个Javascript/Razor问题?,c#,jquery,asp.net-mvc-4,razor,C#,Jquery,Asp.net Mvc 4,Razor,我尝试添加以下javascript代码 <script> @if (ViewBag.checkedArtikel != null) { foreach (int ac in ViewBag.checkedArtikel) { String temp = "'#addartikel" + ac + "'"; <text> $(@temp).toggleClass('down'

我尝试添加以下javascript代码

 <script> 
    @if (ViewBag.checkedArtikel != null)
    {
        foreach (int ac in ViewBag.checkedArtikel)
        {
            String temp = "'#addartikel" + ac + "'";
            <text> $(@temp).toggleClass('down');</text>
        }
    }
    </script>
但是使用script标记,我得到了以下错误:

Uncaught SyntaxError: Unexpected token &
使用此代码:

<text>$(@Html.Raw(temp)).toggleClass('down');</text>
或者,您可以使用旧代码而不向变量添加引号:

String temp = "#addartikel" + ac;
<text> $('@temp').toggleClass('down');</text>
使用此代码:

<text>$(@Html.Raw(temp)).toggleClass('down');</text>
或者,您可以使用旧代码而不向变量添加引号:

String temp = "#addartikel" + ac;
<text> $('@temp').toggleClass('down');</text>

您将服务器端Razor代码与客户端javascript严重混合。通过使用JSON序列化程序,以下是正确的方法:

<script type="text/javascript">
    var articles = @Html.Raw(Json.Encode(ViewBag.checkedArtikel ?? new int[0]));
    $(articles).each(function() {
        $('#addartikel' + this).toggleClass('down');
    });
</script>

您将服务器端Razor代码与客户端javascript严重混合。通过使用JSON序列化程序,以下是正确的方法:

<script type="text/javascript">
    var articles = @Html.Raw(Json.Encode(ViewBag.checkedArtikel ?? new int[0]));
    $(articles).each(function() {
        $('#addartikel' + this).toggleClass('down');
    });
</script>