4

Here is what I would like to express by Razor:

<b>@parameterMapping.Title</b> (Category: @parameterMapping.Category.Title, Regexp: @parameterMapping.Regexp)

But to make it parseable I have to write it this way:

<b>@parameterMapping.Title</b> <text>(Category: </text> @parameterMapping.Category.Title <text>, Regexp:</text> @parameterMapping.Regexp <text>)</text>

Are there better ways to solve this problem?

3
  • What are the datatypes for each of those properties? Commented Sep 15, 2010 at 22:01
  • What error do you get when you say it's "not parseable"? Commented Sep 15, 2010 at 22:08
  • @Larsenal: All properties are strings. I get a strange error saying that there is no <div> for closing </div>. If I remove those lines it works okay. Commented Sep 15, 2010 at 22:18

1 Answer 1

13

What you wrote down:

<b>@parameterMapping.Title</b>
(Category: @parameterMapping.Category.Title, Regexp: @parameterMapping.Regexp)

Is actually valid in Razor. My guess is that you have all of this in some conditional or iterative statement (if or foreach etc). In this case you can wrap the whole thing in <text>:

@if(Foo) {
  <text><b>@parameterMapping.Title</b>
  (Category: @parameterMapping.Category.Title, Regexp: @parameterMapping.Regexp)</text>
}

Instead of <text> you could use a valid HTML element like <p> or <div>. This is because by default after the { the parser is sitll in "code" mode and needs a markup tag to switch to "markup" mode.

Note that Razor performs tag matching which is why you need to have the whole scope of the if statement contained in a tag if you want all of it to be treated as markup. Otherwise everything that's not inside of a tag would be treated as code:

@if(Foo) {
  // Treate as code
  <text>
    Markup
    <div>More markup</div>
    @EscapeIntoCode
  </text>
  // Treate as code again
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Idsa any tag will do. <text> is a magic tag that does not get rendered to the output.
But according to the code in my question, <b> tag didn't switch razor to markup mode.
@Idsa I added clarification that should answer your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.