Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

asp.net mvc 4 - Why is MVC 4 Razor escaping ampersand when using HTML.Raw in a title attribute

We recently upgraded to MVC 4 and now we are having titles in our links not display correctly. The problem is before HTML.Raw would not escape & in our title attributes, but now it does. Below is my sample code:

<a title="@Html.Raw("Shoe Size 6&#189;-8")">Test</a>

Which produces the following markup:

<a title="Shoe Size 6&amp;#189;-8">Test</a>

The only solution I found so far was to put the entire anchor into a string and then HTML.Raw that string.

Why is Html.Raw escaping ampersand in anchor tag in ASP.NET MVC 4?.

This is a very ugly solution and I am hoping there is a better alternative.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While it is only a small step less ugly workaround, you can simply @Html.Raw the full attribute name and value.

<a @Html.Raw("title="Show Size 6&#189;-8"")>Test</a>

Results in:

<a title="Show Size 6&#189;-8">Test</a>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...