5

I have experienced a very strange issue which I cannot find the cause of.

In my Asp.Net MVC app (.net4.0/MVC4) I am rending html data attributes within some html element to then be used within the JavaScript.

So in the app I have a Model, e.g.

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

I am then passing this model through onto a simple MVC view page and rendering the boolean value into the html data attribute, e.g.

@model MyProject.MyModel

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

Now when running the project locally the html is rendered as:

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

However when running on the server, the html is rendered as:

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

At first I thought that maybe the boolean was not being set somehow, so I added this to the element Click Me @Model.MyFlag which renders as Click Me True. Now I suspected that this has maybe something to do with Debug vs Release mode, however after playing about with this it made no difference.

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

Any ideas what is the cause of this?

Tim B James
  • 20,084
  • 4
  • 73
  • 103
  • Were you using the same browser when testing locally vs testing the deployed version? – Josh Darnell Aug 13 '15 at 14:49
  • @jadarnel27 yeah same browser. Also tried alternate browsers. – Tim B James Aug 13 '15 at 15:32
  • 3
    Is it possible you somehow have a different version of the Razor view engine running locally vs. the server? The behavior you see on the server looks kind of like the conditional html attributes feature introduced in Razor 2. – Peter Aug 13 '15 at 15:34
  • 2
    I'm not 100% sure on the cause of this difference, but it would seem that the HTML rendered when the project is deployed on the server is correct. Have a look here: http://stackoverflow.com/a/4140263/3130094 – Jamie Dunstan Aug 13 '15 at 15:38
  • Thanks all for your comments. Looks like it is a MVC versioning difference locally and on the server. – Tim B James Aug 13 '15 at 15:47

1 Answers1

3

I quote the answer from another website:

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

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

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

zed
  • 2,298
  • 4
  • 27
  • 44