How do I move boolean logic out of my View?

asp.net-mvc, c#

Solution

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

Problem

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?

Original source