Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor View Syntax doesn't recognize the "@" in an HTML attribute

I am migrating a project from MVC 2 to MVC3 and the razor view engine.

In MVC 2, I would have the following html:

<div id="del_<%= Model.ActivityID.ToString() %>"></div>

When using razor, I tried the following, which renders the literal text "[email protected]()" when I want del_1.

<div id="[email protected]()"></div>

To get around the issue, I used:

<div id="@Model.ActivityID.ToString()_del"></div>

Is there away to make razor work with this syntax?

<div id="[email protected]()"></div>
like image 939
scottrakes Avatar asked Dec 07 '10 13:12

scottrakes


People also ask

What is razor view HTML?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language.

Which of the following represents Razor syntax?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML.

How do I add Razor view?

Right-click on the Views/HelloWorld folder, and then Add > New Item. In the Add New Item - MvcMovie dialog: In the search box in the upper-right, enter view. Select Razor View - Empty.


2 Answers

You'll have to use the @() around your particular model value like so:

<div id="del_@(Model.ActivityID.ToString())"></div>

The reason for this is because the [email protected] looks like an email address to the parser and by default the parser tries to ignore email addresses so you don't have to do something silly like john@@doe.com as emails are common enough that it would be annoying to do every time. So the people working on the razor parser just figured: "if it looks like an email, ignore it". So that's why you're having this particular issue.

like image 96
Buildstarted Avatar answered Sep 20 '22 00:09

Buildstarted


<div id="del_@(Model.ActivityID.ToString())"></div>

In case you didn't see the trick: use @( )

like image 30
GvS Avatar answered Sep 21 '22 00:09

GvS