views:

130

answers:

2

Whats the best way to print out "Yes" or "No" depending on a value

In my view I want to print out

Model.isStudent

and I dont want True or False, I want Yes or No.... do I Have to write if else statement?

+7  A: 

Write a helper method:

public static class MyExtensions
{
    public static string FormatBool(this HtmlHelper html, bool value)
    {
        return html.Encode(value ? "Yes" : "No");
    }
}

And use it like this:

<%= Html.FormatBool(Model.IsStudent) %>
Darin Dimitrov
+3  A: 

How about an extension method on bool:

public static class BoolExtensions {
    public static string ToYesNo(this bool value) {
        return value ? "Yes": "No";
    }
}

Usage would be:

Model.isStudent.ToYesNo();
jrotello