如何在MVC Razor DropDownList中使用Html.Raw? [英] How to use Html.Raw in MVC Razor DropDownList?
问题描述
我在MVC Razor中使用DropDownList,并且在自动HTML编码方面遇到问题.我的代码如下:
I'm using DropDownList in MVC Razor, and am having problems with the automatic HTML encoding. My code looks like:
@Html.DropDownList("MyList", Model.DropdownNamesAndValues)
可以正常工作,除了DropdownNamesAndValues
列表中的SelectListItem.Text
属性可以在其中包含HTML粗体或斜体标签.这些当前实际显示在下拉列表中(例如<i>hello</i> world
).我想知道怎么做:
Which works fine, except that a SelectListItem.Text
property in the DropdownNamesAndValues
list may have HTML bold or italic tags in it. These are currently appearing literally in the dropdown (e.g. <i>hello</i> world
) . What I'd like to know how to do is:
- 如何将@ Html.Raw应用于每个
SelectListItem.Text
属性? - 失败了,有没有一种简单的方法可以删除HTML标签呢?基本上什么都没有,而不是像现在这样按字面意思显示.
- How to apply @Html.Raw to each of the
SelectListItem.Text
properties ? - Failing that, is there an easy way to just remove the HTML tags instead ? Basically anything instead of showing them literally as at present.
推荐答案
您应在视图之前将html标记剥离.在控制器中,或者最好在模型中,希望您能获得这些值.
You should strip the html tags out before the view. In either the controller or preferably in the model where you are hopefully getting the values.
这篇关于如何在MVC Razor DropDownList中使用Html.Raw?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!