digita1-anal0g
digita1-anal0g

Reputation: 521

How do I escape curly braces in Asp .Net MVC Razor?

I'm trying to bind a data attribute as follows:

@Html.Label("test", new { data_test = "{{vm.test}}" })

When rendered, what I see is:

<label data-test for="test">test</label>

How do I escape the curly braces so that they're rendered on the page? Some things a I've tried are:

Upvotes: 2

Views: 4091

Answers (3)

Lodlaiden
Lodlaiden

Reputation: 391

If you need to display the curly braces, as when you're explaining handlebar logic and you don't need any other constructs:

Code:

<note>Example: @("{{object.exampleField}}")</note>

Displayed:

Example: {{object.exampleField}}

Upvotes: 1

user9123700
user9123700

Reputation:

To write curly braces "{}" in razor use the combination of "at" and "colon". Example: @:{

I had just figured it out while using handle bars.

And to write "@" use one more "at" sign.

Example: @@

I hope it helps the new ones who are looking for it!

Upvotes: 0

IndieTech Solutions
IndieTech Solutions

Reputation: 2539

Inside a code block, you cannot use @ characters to create more code blocks. something like:

@Html.Label("test", new { data_test = "{{@vm.test}}" })

Upvotes: 0

Related Questions