如何覆盖 DisplayFor 布尔值? [英] How do I override DisplayFor boolean?

查看:26
本文介绍了如何覆盖 DisplayFor 布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

我必须创建类似的东西,以便显示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:

 ViewsSharedDisplayTemplatesBoolean.ascx

并添加如下代码:

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

希望这有帮助!

编辑忘记了,在你看来,只是这样称呼它:

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

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

编辑 2对于可为空的(布尔值?),试试这个:

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

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

编辑 3使用 Razor 语法 (ViewsSharedDisplayTemplatesBoolean.cshtml):

EDIT 3 Using Razor syntax (ViewsSharedDisplayTemplatesBoolean.cshtml):

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

这篇关于如何覆盖 DisplayFor 布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆