tags:

views:

45

answers:

3

I'm rendering a drop down where the text rendered in the drop down is too long for comfort. How can I go about limit the number of characters that display in the dropdown without having to go back to the controller?

<option value="<%=order.ID %>"><%=order.Item %></option>

thx

A: 

Use a substring on the order.item. If you want the max to be, say, 20 characters then choose the first 17 characters concatenated with '...'

Jason Irwin
+3  A: 
<%= order.Item.Length > 10 ? order.Item.Substring(0, 7) + "..." : order.Item %>

As Dan mentions below, checking for null and pulling out into a helper method is a good idea.

public static class StringHelpers
{
    public static string SmartSubstring(string value, int maxLength)
    {
        if(String.IsNullOrEmpty(value))
            return String.Empty;

        if(value.Length > maxLength)
            return value.Substring(0, maxLength -3) + "...";

        return value;
    }
}

Then just make sure you include this class at the top of your aspx, and you can do:

<%= StringHelpers.SmartSubstring(Order.Item, 10) %>
Matt
I don't like this, because a null string will cause an object reference exception here. You're best checking whether the string is not null or empty before checking the length of it. Also, that's a fair chunk of logic in the view that could be moved into a helper, and would promote reuseability as this sort of thing is useful in lots of places.
Dan Atkinson
@Matt! Good answer! If you change it to "this string value" then it'll work as an extension, so you can just do "Order.Item.SmartSubstring(10)".
Dan Atkinson
This kind of stuff could also be done in the controller when building the viewmodel to send to the page. That way, you can wrap some tests around it and keep the code out of your view.
Eric King
Also, there might be an issue (as I mentioned in mine) where the string length could be less than three, and thus also cause an error. Maybe when checking for null or empty, checking for a length > 3. If it isn't then return value, or string.Empty if it's null. :)
Dan Atkinson
@eking, I wouldn't put it in the controller myself, as that means that you're handling logic that it shouldn't care about. It should take the input and call models and views. Having it outside of MVC - in a static helper class like Matt has done - doesn't mean you can't make any tests for it though.
Dan Atkinson
@Dan - It's probably a matter of style, but I prefer my views to receive a viewmodel that's already prepared for display. The view itself should not need to manipulate the viewmodel objects it receives just to display them. Doing it this way serves two purposes: the views themselves are simpler, and the viewmodel preparation (in this case, truncation of the order item name) is inherently unit testable. If that logic is delegated to the view itself, it becomes much harder to wrap it in a unit test.
Eric King
+1  A: 

I don't like a lot of logic in my view, and this sort of thing is useful all over (not just in views).

<option value="<%=order.ID %>"><%=order.Item.Truncate(10) %></option>

And the Truncate method...

public static class StringExtensions
{
  public static string Truncate(this string value, int trim)
  {
    //Don't do anything if the value is null, empty, or doesn't meet our trim constraint
    if (string.IsNullOrEmpty(value) || value.Length <= trim)
    {
      return value;
    }

    return string.Format("{0}...", value.Substring(0, trim));
  }
}

Caveat: I haven't trimmed the length of the ellipsis off. If you want to, you should do some additional checking to ensure that (trim-3) is greater than zero Also, you should look at accounting for whether the trim length is greater than the length of the string. I would do, but my brain is shutting down for the night!

Dan Atkinson