如何覆盖 DisplayFor 布尔值?

发布于 2024-11-27 07:37:22 字数 132 浏览 0 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情释 2024-12-04 07:37:23

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

使用内置的 DisplayTextFor() 而不是 DisplayFor()。

这是一篇旧帖子,但我很难找到当前的答案。

@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.

玩物 2024-12-04 07:37:23

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

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

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= {布尔属性} A= {非布尔属性}

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}

陌上芳菲 2024-12-04 07:37:22

我必须创建类似的东西,以便它显示“Sim”和“Não”(葡萄牙语是/否)。我创建了以下文件:

 Views\Shared\DisplayTemplates\Boolean.ascx

并添加了以下代码:

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

希望这有帮助!

编辑
忘了,在你看来,简单地这样称呼它:

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

EDIT 2
对于可为 null(布尔?),请尝试以下操作:

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

编辑 3
使用 Razor 语法 (Views\Shared\DisplayTemplates\Boolean.cshtml):

 @{ Layout = null; }
 @(ViewData.Model ? "Sim" : "Não")

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")
深爱成瘾 2024-12-04 07:37:22

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

@((bool)item.Ativo ? "Yes" : "No")

How about just this simple thing:

@((bool)item.Ativo ? "Yes" : "No")
风流物 2024-12-04 07:37:22

您可以为 bool 扩展 HtmlHelper。

请记住,您必须在 razor page 上使用方向 YesNoExtensions 命名空间。
rem:我们可以通过更改函数符号来重载布尔值的 DisplayFor 。

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);
        }
    }
}

和剃刀页面。

<%@ import namespace='SampleExtensions' %>


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

最后一个参数 true 是虚拟的,用于选择正确的 DisplayFor,它已被我们重载。我希望有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文