Razor View Engine和jQuery [英] Razor View Engine and jQuery
问题描述
是否有人知道如何强制Razor View引擎打印foreach循环下的确切行.代码如下:
does anybody know how to force Razor View engine to print exact line which is under foreach loop. Code follows :
@section head{
<script type="text/javascript" src="@Url.Content("~/Content/Scripts/jquery-1.4.1.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Content/Scripts/jquery.progressbar.min.js")"></script>
<script type="text/javascript">
$(document).ready(function() {
@foreach(var player in Model)
{
jQuery("#pb@PlayerID").progressBar();
}
});
</script>
}
我尝试使用$()和jQuery(),但在两种情况下razor都不知道该怎么做.有什么方法可以迫使他准确地打印出来: jQuery(#pb @ PlayerID").progressBar(); .我想要这样的东西:
I tried using $() and jQuery() but in both case razor don't know what to do. Is there any way to force him to print exact this : jQuery("#pb@PlayerID").progressBar(); . I want to have something like this :
$(document).ready(function() {
$("#pb1").progressBar();
$("#pb2").progressBar();
$("#pb3").progressBar();
});
提前谢谢!
推荐答案
在@foreach块中,默认情况下,内容为代码,除非您切换回标记.因此,"jQuery(...).progressBar()"行被视为C#.在这种情况下,如果您想要的不是HTML标记,则可以使用< text>标记,它实际上并未呈现,或者是"@:"指令,该指令指示Razor将该行的其余部分视为标记,无论其包含什么内容(当然,您可以在行内使用"@"来嵌套更多代码块).
Inside the @foreach block, the content is code by default unless you switch back to markup. So the "jQuery(...).progressBar()" line is considered C#. In cases like this, where you want markup that isn't HTML, you can use the <text> tag, which is not actually rendered OR the "@:" directive which instructs Razor to treat the rest of the line as markup, no matter what it contains (of course, you can then use "@" within the line to nest further code blocks).
此外,"pb @ PlayerID"看起来像是Razor的电子邮件地址,因此将其忽略.您可以通过使用@()显式表达式语法来避免这种情况.因此,@ foreach块应如下所示:
Also, the "pb@PlayerID" looks like an email address to Razor so it ignores it. You can avoid that by using the @() explict expression syntax. So the @foreach block should look like this:
@foreach(var player in Model)
{
@: jQuery("#pb@(PlayerID)").progressBar();
}
这篇关于Razor View Engine和jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!