如何在ASP.NET MVC的下拉列表中发出未编码的html

时间:2011-02-28 20:12:17

标签: asp.net-mvc

我想填充一个带有商标和版权字符的下拉列表,但看起来它们总是被html编码,所以我得到的是它们的编码形式。

感谢您的帮助。

2 个答案:

答案 0 :(得分:5)

填充SelectList时,请在文本上使用HttpUtility.HtmlDecode。这是一个例子:

<%
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) %>

答案 1 :(得分:0)

或者这样,如果Model是SelectList

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