- Posted by liammclennan on February 18, 2009
Do you have any code like this in your views?:
<% if (someThing == someOtherThing) { %>
Conditional content
<% } else { %>
Alternative content
<% } %>
Here is a very simple extension method to help clean up this kind of view code.
public static void WriteIf(this HtmlHelper helper, bool condition, string truePart, string falsePart)
{
helper.ViewContext.HttpContext.Response.Write((condition) ? truePart : falsePart);
}
Then the view can be simplified to this:
<% Html.WriteIf(someThing == someOtherThing, "Conditional content", "Alternative content") %>