2

I'm banging my head against the wall on this one and would like to know why when I enclose an HTML attribute, such as an id tag in single quotes: <div id='test'></div>, they are automatically being replaced with double quotes: <div id="test"></div>, when rendered and sent to the client for viewing.

Is there a way to enforce single quotes in attributes?

So far I have tried @Html.Raw("'test'"), @Html.Raw("id='test'"), and etc, and no matter what I do the quotes are still replaced. I see plenty of examples of single quotes being used successfully without automatic conversion, so I'm wondering what's going on and if anyone can provide resolution to this problem

I'm using ASP.Net MVC 5, Bootstrap 3.0, Jquery, and Less, nothing too exotic in terms of javascript libraries, and the standard Razor view.

Thanks for your answers.

3
  • Do you need the id to be set to test or 'test'? Commented Dec 13, 2013 at 9:30
  • No that is irrelevant and was used to provide a simplified example. The results are the same if the attribute is data-options='two'. Commented Dec 13, 2013 at 9:35
  • If you look at the source via the browser, then you will always see what the browser want's it to be. e.g. IE Always shows you double quotes. This is because the code is being parsed befored it is displayed by the browser. So you're looking at a rerendering of your parsed html. Commented Dec 13, 2013 at 11:34

1 Answer 1

2

The razor engine automatically escapes any HTML output, using Html.Raw tells the engine to just output as is. You would need to do the entire block of HTML using Html.Raw, not just the attribute e.g.

@Html.Raw("<div id='test'></div>")

Alternatively, you could output your attribute as a literal e.g.

<div @("id='active'")></div>

Interestingly it looks as though the browser is pre-processing the HTML and replacing the single quotes with doubles. I checked the result coming down from the server and I got

<div id='active'></div>

However, when I then viewed the HTML in the browser (Chrome 31 / IE 11) the single quotes had been replaced with double. This isn't just a display thing either because when I saved the output from the browser it still had double quotes.

This is probably the norm for most modern-day browsers and, unfortunately, it doesn't look like you have much control over this.

Sign up to request clarification or add additional context in comments.

8 Comments

Neither of those examples worked... Using a clean MVC 5 app, the first method resulted in <div id="test"></div> and the second method resulted in <div id="'active'"></div>.
@SunshineAttack hmm that's odd, maybe MVC5 has changed how Raw works but I am sure it should output as is. What about if you do <div @MvcHtmlString.Create("id='active'")></div>?
@SunshineAttack or try <div [email protected]("'active'")></div>
They both ended up as <div id="active"></div>.
@SunshineAttack not sure why they are being escaped...how about @MvcHtmlString.Create("<div id='active'></div>")?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.