如何在MVC Razor DropDownList中使用Html.Raw? [英] How to use Html.Raw in MVC Razor DropDownList?

查看:51
本文介绍了如何在MVC Razor DropDownList中使用Html.Raw?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆