2010-12-16 109 views
62
@foreach (string s in "1,2,3".Split(',')) { 
    s is equal to @s<br/> 
} 

我要吐了出來: s等於1 s等於2 s等於3剃刀語法 - foreach循環

但我發現了各種因視覺誤差的Studio認爲{}之間的內容是代碼,但我希望它是標記。

回答

107

剛看到這個on ScottGu's blog this morning:使用@:在該行之前:

@foreach (string s in "1,2,3".Split(',')) { 
    @: s is equal to @s<br/> 
} 

或者,使用<text />標籤:

@foreach (string s in "1,2,3".Split(',')) { 
    <text>s is equal to @s<br/></text> 
} 
+0

''對多行很好。 – crush 2014-10-28 17:52:54

5
@foreach (string s in "1,2,3".Split(',')) { 
    <text>s is equal to </text>@s<br/> 
} 

我想這是因爲你在括號外的解析文本,以便剃刀想它的代碼,請嘗試使用上面的剃刀文本標籤,這種分析完全一樣@ :但(至少對我來說)更直觀一些(它不會解析標籤)