MVC 3剃须刀如何使复杂的JavaScript的条件? [英] MVC 3 Razor how to make complex javascript conditional?

查看:104
本文介绍了MVC 3剃须刀如何使复杂的JavaScript的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了这个多行JavaScript代码:

I've got this multi-line javascript snippet:

$.getJSON('@Url.Action("ReconBases")', 
          { modelId: selectedModelId },
          function(selectItems) { 
            buildDropDown('#SelectedReconId', selectItems); 
          });

我想这个脚本有条件地添加到页面基于视图模型变量,就像这样:

I want to conditionally add this script to the page based on a view model variable, like this:

@if ( Model.GetBases )
{
  <snippet above>
}

谁能告诉我,如果这是可能的,这样做的正确的语法?我使用@尝试:与Html.Raw,但我似乎无法得到正确的格式为它工作。

Can anyone tell me if this is possible and the right syntax for doing this? I've tried using @: and Html.Raw, but I can't seem to get the right format for it to work.

推荐答案

输出$ C $里面剃刀条件ç必须用HTML标签包围,如&LT; D​​IV&GT;&LT; / DIV&GT; (以下简称标志,它作为输出,并没有更多的C#code)。

Output code inside a conditional in Razor must be surrounded by HTML tags such as <div></div> (this "flags" it as output, and not more C# code).

如果没有特别的标签适合你的需要,你可以使用特殊&LT;文本&GT;&LT; /文字和GT; 是剃刀具体。这些是不会渲染时输出。

If no particular tag suits your needs you can use the special <text></text> that are Razor specific. These are NOT outputted during rendering.

@if ( Model.GetBases )
{
  <text>
    <snippet above>
  </text>
}

这也适用于的foreach

这篇关于MVC 3剃须刀如何使复杂的JavaScript的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆