【问题标题】:Displaying a check instead of a bool显示支票而不是布尔值
【发布时间】:2016-10-26 15:21:26
【问题描述】:

我正在使用 MVC 并有一个索引视图。显示的字段之一是 Bool 类型。目前,它将显示其值的 True/False 但是有没有办法在 True 时显示复选标记或选中的复选框,如果为 False 则不显示?

我的领域 -

public bool PrimaryContact { get; set; }

我的观点 -

@foreach (var item in Model.Contacts)
        {
            <tr>
                <td>@item.PrimaryContact</td>
                <td>@item.ContactType</td>
                <td>@item.Contact1</td>
           </tr>
      }

【问题讨论】:

    标签: asp.net-mvc boolean


    【解决方案1】:

    如果你绝对想显示一个复选框,你可以这样做。

    <td> <input type="checkbox" checked="@item.PrimaryContact" />  </td>
    

    但复选框是表单输入控件。因此,如果这是一个仅查看屏幕,为什么不简单地打印“是”或“否”而不是输入控件。

    <td>@(item.PrimaryContact?"Yes":"No")</td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      相关资源
      最近更新 更多