【问题标题】:ASP.NET MVC - Change BG Color of a table if a boolean property is set as falseASP.NET MVC - 如果布尔属性设置为 false,则更改表的 BG 颜色
【发布时间】:2019-06-29 05:55:47
【问题描述】:

当我的“Active”属性切换为 false 时,如何更改表格行的颜色?

我不使用表中的“Active”属性,因为它没有意义。那么我将如何检查该属性是否为 false 并更改表格的颜色?

我的视图模型

 public class CodigosDeOperacaoViewModel
  {
   [Key]
   public Guid Id { get; set; }

    [Required(ErrorMessage ="Campo Obrigatório")]
    [MaxLength(10)]
    public string Code{ get; set; }

    [Required(ErrorMessage = "Campo Obrigatório")]
    public string Description{ get; set; }

   [ScaffoldColumn(false)]
   public bool Active{ get; set; }
  }

我的视图

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Code)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>

        <th>Actions</th>
    </tr>

    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Code)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
    </tr>
    }   
</table>

【问题讨论】:

    标签: asp.net-mvc twitter-bootstrap razor


    【解决方案1】:

    您的视图模型有点奇怪,您发布的代码不起作用。您将需要对其进行更新,以便它包含有关您的行的数据。像这样的

     public class CodigosDeOperacaoViewModel
     { 
       //ROWs data
       public ICollection<RowData> RowItems {get;set;}
    }
    
    public class RowData
     {
        [Key]
        public Guid Id { get; set; }
    
       [Required(ErrorMessage ="Campo Obrigatório")]
       [MaxLength(10)]
       public string Code{ get; set; }
    
       [Required(ErrorMessage = "Campo Obrigatório")]
       public string Description{ get; set; }
    
       [ScaffoldColumn(false)]
       public bool Active{ get; set; }
    }
    
    
    
    <table class="table">
    <tr>
        <th>
            Code
        </th>
        <th>
            Description
        </th>
    
        <th>Actions</th>
    </tr>
    
    @foreach (var item in Model.RowItems)
    {
    <tr @(item.Active? "active": string.Empty)>
        <td>
            @Html.Display(item.Code)
        </td>
        <td>
            @Html.Display(item.Description)
        </td>
    </tr>
    }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 2019-02-18
      相关资源
      最近更新 更多