Html MVC Data Attribute Difference

I had a very strange problem, because of which I can not find the reason.

In my ASP.NET MVP application (.net4.0 / MVC4) I am smoothing out the html data attributes inside some html element so that I can use it in JavaScript.

So, in the application, I have a model, for example

public class MyModel{ public bool MyFlag { get; set; } } 

Then I pass this model to a simple MVC lookup page and pass the boolean to the html data attribute, for example.

 @model MyProject.MyModel <a href="#" data-is-flagged="@Model.MyFlag">Click Me</a> 

Now when you start the project locally, html is displayed as:

 <a href="#" data-is-flagged="True">Click Me</a> 

However, when launched on the server, html is displayed as:

 <a href="#" data-is-flagged="data-is-flagged">Click Me</a> 

At first, I thought that maybe the boolean value was not set in some way, so I added it to the Click Me @Model.MyFlag , which appears as Click Me True . Now I suspected that this might be due to the Debug vs Release mode, however, after playing with it it made no difference.

My fix was to change the code to output a boolean as a string value, for example. data-is-flagged="@Model.MyFlag.ToString()" , which then displays the same locally on the server.

Any ideas what is the reason for this?

+5
source share
1 answer

I quote the answer from another site :

This is the result of Conditional Attributes that was introduced in Web Pages 2 (MVC 4): http://www.mikesdotnetting.com/Article/201/Cleaner-Conditional-HTML-Attributes-In-Razor-Web-Pages
Two options: return to web pages 1 (MVC 3) or edit all files.

If the value applied to the attribute is true , the result is that the attribute is repeated (this is useful for option tags inside select ). If set to false , nothing is displayed (not the name of the event attribute).

So, as @Jamie and @Peter comments, you may have a different version of the Razor engine in your development environment.

+3
source

All Articles