开发者

How to emit unencoded html in the dropdown list in ASP.NET MVC

I want to populate a dropdown list with trademark and copyright characters but looks like th开发者_如何学Cey are always html encoded so what I get instead is their encoded form.

Thanks for your help.


When populating your SelectList, use HttpUtility.HtmlDecode on the text. Here's an example:

<%
var entities = new string[] { "&copy;", "&lt;&quot;&gt;", "&#169;" };
var selectListItems = entities.Select(e => new SelectListItem { 
                                    Text = HttpUtility.HtmlDecode(e), Value = "1" 
                               });
%>

<%= Html.DropDownList("exampleDropDownList", selectListItems) %>


Or this way, if Model is SelectList

    @Html.DropDownList("DropDownList", Model.Select(i => { i.Text = HttpUtility.HtmlDecode(i.Text); return i; }))
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜