@foreach (var item in Enumerable.Range(0, 10)) { user } - here, if you don't put the tag, compiler will assume the whole block is C#, try to find a variable user which does not exist, throws error. The tag is to say clearly that 'user' is just a text which we want in our html.
It can be done with <p>, <span>
or any other existing html tag, but that will put each text in a paragraph (with extra spacing and all) or other markups - which you might not want. See, the point is, if you already have some html tag, compiler will understand that as html and treat properly. But, in cases like the example by @AlexK where you don't want extra html elements, just want to put some plain text - there you use to distinguish it from C# codes.
The first line in the other answer, you can do the same thing with @: instead of tag. They do the same thing, make C# compiler differentiate html from code block. looks more html-ish & is more handy in cases where you have multi-line text. Refer ScottGu's blog
Context switching here means going back and forth between C# code block and html markup while generating a razor view page. The code we write for asp.net mvc views are combination of C# and html codes e.g.
1. <ul>
2. @foreach (var p in persons)
3. {
4. <li>
5. @p.Name
6. <a href="some_url/@p.Id">details</a>
7. </li>
8. }
9. </ul>
where line 1, 4, 7, 9 are html, 2, 3, 5, 8 are C# and line 6 is both. So while compiling, the compiler has to clearly understand what is code and what is markup! So, based on some symbols (@ for code, tags for html etc) compiler goes back and forth from C3 to html and create a final html.
The unique thing about the <text>
tag is, it is not a standard html element, but can be used in razor view in the same way. It just encloses a block of plain text that will be rendered in generated html without any html markup.
user
` inside the loop? – Jul 27 '13 at 15:35user
` inside the loop? – Jul 27 '13 at 15:37, but that will put each text in a paragraph (with extra spacing and all) - which you might not want. See, the point is, if you already have some html tag, compiler will understand that as html and treat properly. But, in cases like the example by @AlexK where you don't want extra html elements, just want to put some plain text - there you use to distinguish it from C# codes.
– Arghya C Jul 27 '13 at 15:49