对于嵌套和混合代码块,正确的razor语法是什么?
本文关键字:razor 是什么 语法 代码 于嵌套 嵌套 混合 | 更新日期: 2023-09-27 18:03:50
我有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>
}
这一行有一个额外的@
:
@if (ViewBag.Searched)
和这一行:
@if (ViewBag.PendingRequest)
因为你已经在razor代码中了(由父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总是很差地缩进razor视图:)