How to use Html.Raw in MVC Razor DropDownList?

I am using DropDownList in MVC Razor and I am having problems with automatic HTML encoding. My code looks like this:

@Html.DropDownList("MyList", Model.DropdownNamesAndValues)

This works just fine, except that the property SelectListItem.Textin the list DropdownNamesAndValuesmay contain HTML bold or italic tags. Currently, they appear literally in a drop-down list (e.g. <i>hello</i> world). I would like to know how to do this:

  • How to apply @ Html.Raw to each of the properties SelectListItem.Text?
  • Otherwise, is there an easy way to simply remove the HTML tags instead? In principle, nothing, instead of showing them literally as they are at present.
+5
source share
4 answers

You must unmark the html before the view. In the controller or, preferably, in the model where you hope to get values.

+1
source

Write to the controller a method that looks for subsets <*> and </*>in your lines and removes them.

I don’t think you can add html tags to the drop down list.

+2
source

, html , view

+2

, HTML , DropDownList, HttpUtility.HtmlEncode() ListItemToOption. , , ( ) .

+1
source

All Articles