![](/img/trans.png)
[英]How to create Calendar control using jquery in MVC3 aspx view engine
[英]Using conditional compile constants to control JavaScript emitted by an MVC3 view
我已经收集到可以在Razor标记中使用条件编译器指令和常量。 我想知道是否有办法使用这些指令有条件地发出JavaScript文本,正如我所寻求的那样:
@section BodyPanelScript
{
<script type="text/javascript">
$(function () {
$(":password").val(null);
// Back up current names of package radio buttons, then make all their names the same for grouping.
$("#package-selector :radio[name$='.IsSelected']").each(function () {
$(this).attr("oldname", $(this).attr("name"));
});
$("#package-selector :radio[name$='.IsSelected']").attr("name", "Package.IsSelected");
// Hook the 'submit' click to restore original radio button names.
$("form#register :submit").click(function () {
$(":radio[name='Package.IsSelected']").each(function () {
$(this).attr("name", $(this).attr("oldname"));
});
});
});
</script>
@{
#if AUTO_FILL_REG
<text>
<script type="text/javascript">
$(function () {
$(":password").val("123456");
});
</script>
</text>
#endif
}
}
常量AUTO_FILL_REG
在我的Debug配置中定义,并告诉控制器使用默认值填充注册模型,因此我不必总是完成表单来测试该过程。 密码字段中忽略模型中的现有值,因此我使用一些JavaScript来填充此字段。 我希望有条件地发出这个JavaScript,但是像上面代码这样的几次尝试都会导致#if...#endif
文本被逐字渲染,并且无论构建配置如何,都会填充密码或不渲染任何内容。
这不是一个特别优雅的解决方案,但它很简单。 你可以做一个像这样的简单课程
public static ConditionalValue {
public static bool AUTO_FILL_REG {
get {
#if DEBUG
return true;
#else
return false;
#endif
}
}
}
然后在你的aspx中引用它。 这只是您可以放置此属性的一个示例。 你也可以把它作为常数来做,但你明白了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.