razor syntax - foreach loop

前端 未结 3 1264
死守一世寂寞
死守一世寂寞 2021-02-06 20:19
@foreach (string s in \"1,2,3\".Split(\',\')) {
  s is equal to @s
}

I want to spit out: s is equal to 1 s is equal to 2 s is equal to 3

相关标签:
3条回答
  • 2021-02-06 20:41

    Scott Guthrie just answered that this morning.
    Change it to

    @foreach (string s in "1,2,3".Split(',')) {
      @: s is equal to @s<br/>
    }
    
    0 讨论(0)
  • 2021-02-06 20:43
    @foreach (string s in "1,2,3".Split(',')) {
      <text>s is equal to </text>@s<br/>
    }
    

    I think it is because you are parsing text outside of brackets so Razor is thinking it is code, try using the razor text tag above, this parses exactly the same as @: but (for me at least) is a bit more intuitive (it won't parse the tags)

    0 讨论(0)
  • 2021-02-06 20:56

    Just saw this on ScottGu's blog this morning: use @: before that line:

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

    Alternately, use the <text /> tag:

    @foreach (string s in "1,2,3".Split(',')) {
      <text>s is equal to @s<br/></text>
    }
    
    0 讨论(0)
提交回复
热议问题