Tuesday, October 23, 2012

Create Custom Display Template for Boolean Datatype in ASP.NET MVC


Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://Xplore2WinSqlServer.blogspot.com/
   
Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com

Explore the Top Microsoft Blazor Technical/ Interview Questions here: https://XploreBlazor.blogspot.com/

The ASP.NET MVC's @Html.DisplayFor method by default displays a disabled checkbox for boolean values.
Inorder to display the text, 'Yes' or 'No' instead of a checkbox, create the custom display template for boolean types as follows:

1. Create a folder called DisplayTemplates under the folder,  ~/Views/Shared.
2. Create a file called Boolean.cshtml to have the following code:

@model System.Boolean?

@if (Model.HasValue)
{

      if (Model.Value)
      {
         <text> Yes </text>
      }

      else
      {
         <text> No </text>
      }
}

else
{
      <text> &nbsp; </text>

}


Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://Xplore2WinSqlServer.blogspot.com/

Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Worked like a charm!
    Very simple, thank you!

    ReplyDelete