简体   繁体   中英

Using HTML.Encode on a Select List in ASP.NET MVC

In my ASP.NET MVC application I am using a select list control to generate a list for a multiselect widget:

<%=Html.ListBoxFor( m => m.Product.Name, 
           new SelectList( 
               Model.Products.Where( 
               s => !Model.Product.Any( 
                   t => t.Id == s.Id.Value 
                   ) ).OrderBy( t => t.Name ), "Id", "Name", 
                   new { multiselect = "multiselect", @class = "fancySelect products"}     ) ) %>

Which will generate a list of items. The problem is that some of them need encoding:

 <span>C&#333;netic&trade; Technology</span>

If I render this item directly to the UI using a simple response.write I see this:

<p class="c">Cōnetic&trade; Technology</p>

How would I go about integrating an Html.Encode into my select list statement to produce the same encoding result? Or is there a better encoding method that will effect select lists on a global level?

This is MVC 2 btw, so no razor.

You need to encode data before displaying, and it should render as expected. Here you example which we used in our asp.net mvc2 project:

<%= Html.Encode(ViewData["PasswordLength"]) %>

It is in a namespace System.Web.Mvc, and it converts the specified string to an HTML-encoded string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM