how to use text tag in MVC 3 razor

2019-01-19 08:31发布

问题:

i want to use regex on the views i have in MVC 3 page. how i can use

when i wrap them with text tag they not work ex:

<text> var pattern = @fjkfdkl</text>

i not want to put @@ instead of @ on every pattern. well what is the way and rule for using text tag in MVC

回答1:

When you wrap something in a text tag your are saying to Razor that "this is text" not code. If you want code you can then do a code block like:

<text>@{ var pattern = fjkfdkl; }</text>

If you are doing this in some sort of loop you can just continue writing your code:

foreach(var o in listOfObjects) {
  var pattern = fjkfdkl;
}

In the above example razor knows whats code and what is not. You can then expand on the above example if you want to put markup in the loop:

foreach(var o in listOfObjects) {
  var pattern = fjkfdkl;
  <text>
    Hello World!
  </text>
}

or

foreach(var o in listOfObjects) {
  var pattern = fjkfdkl;
  <p>
    Hello World.
  <p>
}

You only really need to use the <text></text> tags inside of loops where you don't have any html tags.

Razor is smart enough so when you open your tag inside a loop e.g. <p> it know until that tag is closed then its in markup. When it is closed it will then look for a } for the closing of a loop (or another html tag).