Razor if语句与JSON

时间:2016-04-05 13:27:58

标签: json asp.net-mvc razor

我需要在Razor视图中更改某些静态JSON数据的输出。我想要做的就是

, actions: { // <- this is JSON curly bracker
      listAction: '/Operations/Get'
    @if(ViewBag.AllowInput)
    { // <- this is Razor curly bracker
    , createAction: '/Operations/Create'
    , deleteAction: '/Operations/Delete'
    } // <- this is Razor curly bracket
} // <- this is JSON curly bracker

然而,JSON标记与Razor花括号混淆。有没有其他语法,或者,或许,我做了一些非常愚蠢的错误?

1 个答案:

答案 0 :(得分:3)

用户<text>代码或@:行转义:

<text>
, actions: { // <- this is JSON curly bracker
      listAction: '/Operations/Get'
</text>
    @if(ViewBag.AllowInput)
    { // <- this is Razor curly bracker
    <text>
    , createAction: '/Operations/Create'
    , deleteAction: '/Operations/Delete'
    </text>
    } // <- this is Razor curly bracket
<text>
} // <- this is JSON curly bracker
</text>

, actions: { // <- this is JSON curly bracker
      listAction: '/Operations/Get'
    @if(ViewBag.AllowInput)
    { // <- this is Razor curly bracker
@:    , createAction: '/Operations/Create'
@:    , deleteAction: '/Operations/Delete'
    } // <- this is Razor curly bracket
} // <- this is JSON curly bracker