开发者

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

开发者 https://www.devze.com 2023-01-29 01:49 出处:网络
I am migrating a project from MVC 2 to MVC3 and the razor view engine. In MVC 2, I would have the following html:

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 followi开发者_运维知识库ng, which renders the literal text "del_@Model.ActivityID.ToString()" when I want del_1.

<div id="del_@Model.ActivityID.ToString()"></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="del_@Model.ActivityID.ToString()"></div>


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 del_@Model.ActivityID 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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消