Razor View syntax does not recognize "@" in HTML attribute

I am moving the project from MVC 2 to MVC3 and the razor viewer mechanism.

In MVC 2, I will have the following html:

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

When using a razor, I tried the following, which displays the literal text " del_@Model.ActivityID.ToString ()" when I want del_1.

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

To get around the problem, I used:

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

Is there any way to get a razor to work with this syntax?

 <div id=" del_@Model.ActivityID.ToString ()"></div> 
+6
asp.net-mvc razor asp.net-mvc-migration
source share
2 answers

You will need to use @() around your specific model value as follows:

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

The reason for this is that del_@Model.ActivityID looks like an email address for the parser, and by default the parser tries to ignore email addresses, so you don’t have to do something stupid like john@ @doe.com , because emails are common enough that it would be unpleasant to do every time. So the people working on the razor parser just thought, β€œIf this is like email, ignore it.” So why do you have this particular problem.

+11
source share
 <div id=" del_@ (Model.ActivityID.ToString())"></div> 

If you don't see the trick: use @( )

+2
source share

All Articles