Help with c# and bool on asp.net mvc

前端 未结 3 835
有刺的猬
有刺的猬 2021-01-04 20:47

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,

3条回答
  •  鱼传尺愫
    2021-01-04 21:34

    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) %>
    

提交回复
热议问题