How do I override DisplayFor boolean?
Asked Answered
D

5

23

How do i create a display template so i can display a bool as Yes or No not a checkbox? Using mvc3

<%: Html.DisplayFor(model => model.SomeBoolean)%>
Desperate answered 29/7, 2011 at 8:43 Comment(0)
H
25

I had to create something similar so it would display "Sim" and "Não" (portuguese Yes/No). I created the following file:

 Views\Shared\DisplayTemplates\Boolean.ascx

And added the following code:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%= (bool) ViewData.Model ? "Sim" : "Não" %>

Hope this helps!

EDIT Forgot, in your view, simply call it like so:

<%= Html.DisplayFor(i => item.Ativo) %>

EDIT 2 For a nullable (bool?), try this:

<%= (ViewData.Model == null) ? "NA" : (ViewData.Model == true) ? "Y" : "N"%>

EDIT 3 Using Razor syntax (Views\Shared\DisplayTemplates\Boolean.cshtml):

 @{ Layout = null; }
 @(ViewData.Model ? "Sim" : "Não")
Hangnail answered 29/7, 2011 at 19:52 Comment(5)
I had to add a cast to a boolean like this: <%= (bool) ViewData.Model ? "Yes" : "No" %> Thanks, this worked great!Lorgnon
Just a suggestion: try to avoid nullable booleans. Many times bool? = false is, for the application, the same as bool = false. Also, many times the end user cannot distinguish between a null value and a false one (for the boolean datatype). Essentially, if you're using checkboxes to control your booleans, skip the null. If you need the tri-state (null, true, false) you'll be using some sort of dropdown or a fancy checkbox that specifically indicates the absence of a value!Cootie
I'm struggling to convert this to Razor syntax. Can anybody help?Headsail
Agree with @TiagoMatias, don't see a case for nullable checkboxes.Hangnail
Short and sweet, that worked for me! Thanks @HangnailZantos
L
14

How about just this simple thing:

@((bool)item.Ativo ? "Yes" : "No")
Langur answered 30/9, 2011 at 17:17 Comment(0)
N
7

you can extend HtmlHelper for bool.

and remember you must use direction YesNoExtensions namespace on razor page . rem:we can overload DisplayFor for boolean with change function sign.

public namespace SampleExtensions
{
    public static class YesNoExtensions
    {
        public static MvcHtmlString DisplayFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, bool flag = true)
        {
            object o = expression.Compile().Invoke(html.ViewData.Model);
            if (o.GetType() == typeof(bool))
            {
                if ((bool)o)
                    return new MvcHtmlString("Yes");
                else
                    return new MvcHtmlString("No");
            }
            return DisplayFor(html, expression);
        }
    }
}

and razor page.

<%@ import namespace='SampleExtensions' %>


<%: Html.DisplayFor(model => model.SomeBoolean, true)%>

last parameter true is dummy for select right DisplayFor which has been overload by us. I hope usefull.

Nembutal answered 29/7, 2011 at 9:19 Comment(2)
can i not create a new display template under shared and pass in a string value?Desperate
yes, you can do it. remember, to give template name for DisplayFor. it will work.Nembutal
M
6

@Html.DisplayTextFor(model => model.SomeBoolean).

Use the in-built DisplayTextFor() instead on DisplayFor().

This is an old post, but I was having trouble finding a current answer.

Millipede answered 1/4, 2015 at 2:55 Comment(0)
C
0

For true/false use DisplayTextFor by Justin Grant

For yup/nope based on Nuri YILMAZ, this is .NetCore 2.2, to downgrade replace HtmlString with MvcHtmlString:

1) C# write new extension DisplayForYZ

public namespace X.Views.Shared
{
    public static class DisplayExtensions
    {
        // If this was called DisplayFor not DisplayForYZ, we'd get recursion
        public static IHtmlContent DisplayForYZ<TModel, TValue>
          (this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) 
        where TModel : Y
        {
            object o = expression.Compile().Invoke(html.ViewData.Model);
            // For any bool on TModel, return in this custom way:
            if (o.GetType() == typeof(bool))
            {
                return (bool)o ? new HtmlString("Yup") : new HtmlString("Nope");
            }
            // Otherwise use default behaviour 
            return html.DisplayFor(expression);
        }
    }
}

2) cshtml: Import the DisplayExtensions namespace and use new extension.

@model X.ViewModels.Y
@using X.Views.Shared;

@Html.DisplayForYZ(modelItem => modelItem.Z)   @*//Yup/Nope*@
@Html.DisplayForYZ(modelItem => modelItem.A)   @*//default non bool behavior*@

@Html.DisplayFor(modelItem => modelItem.Z)     @*//check box*@
@Html.DisplayTextFor(modelItem => modelItem.Z) @*//true/false*@

X = {my company} Y = {object for customised display} Z= {bool property} A= {non-bool property}

Camouflage answered 11/10, 2019 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.