views:

56

answers:

1

This is a question of style. On date night with my wife she says that I have none and should seek advice.

In my ASP.NET MVC View I pick a sprite based on a boolean value set in the model like this:

<div class="sprite-icon_dog<% =(Model.HasNewDog ? "_new" : "") %>"></div>

This is ugly and I don't like it.

My objective is to use the sprite-icon_dog_new if Model.HasNewDog is true and use sprite-icon_dog if Model.HasNewDog is false.

What is a more elegant and more readable way to do this?

+4  A: 

I think a HTML Helper would be the way to go?

public static string DogDiv(this HTMLHelper html, bool HasDog)
{
  return "...."
}

In your view:

<%=Html.DogDiv(Model.HasDog) %>

Hope that helps,

Dan

Daniel Elliott
I like your answer and will probably mark it as the correct answer soon. To my own detriment I simplified the question too much. There are more classes in the div than I stated so out the box your answer won't work but it's a great idea and starting point that I may be able to expand on - thanks!
Guy