23

如何创建显示模板,以便将布尔显示为是或否而不是复选框?使用 mvc3

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

5 回答 5

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")
于 2011-07-29T19:52:43.763 回答
14

就这么简单的事情怎么样:

@((bool)item.Ativo ? "Yes" : "No")
于 2011-09-30T17:17:06.810 回答
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.

于 2011-07-29T09:19:29.337 回答
6

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

在 DisplayFor() 上使用内置的 Display Text For()。

这是一篇旧帖子,但我无法找到当前答案。

于 2015-04-01T02:55:54.730 回答
0

对于真/假,请使用Justin Grant 的DisplayTextFor

对于基于Nuri YILMAZ 的是/否,这是 .NetCore 2.2,降级用 MvcHtmlString 替换 HtmlString:

1)C#编写新扩展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:导入DisplayExtensions命名空间并使用新的扩展。

@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 = {我的公司} Y = {自定义显示对象} Z = {bool 属性} A = {非 bool 属性}

于 2019-10-11T13:41:01.497 回答