我如何重写DisplayFor布尔? [英] How do I override DisplayFor boolean?

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

问题描述

如何创建一个显示模板,所以我可以显示布尔因为是或否不是一个复选框?使用MVC3

 <%:Html.DisplayFor(型号=> model.SomeBoolean)%GT;


解决方案

我要创建类似的东西所以它会显示SIM卡和NAO(葡萄牙语是/否)。我创建了以下文件:

 视图\\共享\\ DisplayTemplates \\ Boolean.ascx

和增加以下code:

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl<动态>中%GT;
<%=(布尔)ViewData.Model? 辛:NAO%GT;

希望这有助于!

修改
忘了,在你看来,只需调用它像这样:

 <%= Html.DisplayFor(I => item.Ativo)%GT;

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

 <%=(ViewData.Model == NULL)? NA(ViewData.Model ==真)? Y:N%GT;

编辑3
使用剃刀语法(查看\\共享\\ DisplayTemplates \\ Boolean.cshtml):

  @ {布局= NULL; }
 @(?ViewData.Model辛:NAO)

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

解决方案

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

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

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