1

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.

2
  • 1
    MVC is a architectural pattern, ASP.NET MVC 2 is a framework. Do not confuse them. It's like referring to IE as "the internet". Commented Jun 15, 2012 at 22:18
  • 1
    I'm not familiar with this "the internet" thing you speak of... I'll have to get a book on that. Commented Jun 15, 2012 at 22:20

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I would prefer not to put the select list into view data; we don't use view data at all in our application, so it would not pass code review. I appreciate the suggestion though. I'm hoping there is an expression that will help me, or a best practice.
Did you try to encode your data in a service call? In this way you would be able to map your clean(already encoded) data to the ViewModel.
The problem with that approach is the data is encoded correctly for the list, but not the drop down (see the <p> tag? It is encoded right below the ddl) so I can not justify that change in the model :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.