嵌套和混合代码块的正确剃刀语法是什么?

时间:2013-03-14 15:48:30

标签: c# asp.net-mvc razor asp.net-mvc-4 razor-2

我有razor语法嵌套并混合使用html和javascript。

它运行正常,除非我执行自动代码格式,}不断被推到位。

@section js{
    <script type="text/javascript">
        $(document).ready(function () {

            @if (ViewBag.Searched && ViewBag.Found)
            {
                @:$('#found').show(); $('#permit-info').show();

                @if (ViewBag.PendingRequest)
                {
                    @:$('#request-info').hide(); $('#report-info').show();
                }
                else
                {
                    @:$('#request-info').show(); $('#report-info').hide();
                }
            }
            else
            {
                @if (ViewBag.Searched)
                {
                    @:$('#not-found').show();
                            } // <----- PROBLEM. this } doesn't stay at the right place.
            }
        });
    </script>
}

1 个答案:

答案 0 :(得分:3)

此行还有一个@

@if (ViewBag.Searched)

和这一行:

@if (ViewBag.PendingRequest)

因为您已使用剃刀代码(由父if / else语句管理)。

实际上,这仍然会杀死格式。 虽然,但如果您尝试使用<text>代码而不是@:,则可以使用。试试这个:

$(document).ready(function () {

        @if (ViewBag.Searched && ViewBag.Found)
        {
            <text>$('#found').show(); $('#permit-info').show();</text>

            if (ViewBag.PendingRequest)
            {
                <text>$('#request-info').hide(); $('#report-info').show();</text>
            }
            else
            {
                <text>$('#request-info').show(); $('#report-info').hide();</text>
            }
        }
        else
        {
            if (ViewBag.Searched)
            {
                <text>$('#not-found').show();</text>
            }
        }
    });

请注意:Visual Studio总是很难缩小剃刀视图:)

相关问题