【问题标题】:How to apply conditional CSS for numeric field?如何为数字字段应用条件 CSS?
【发布时间】:2013-03-14 15:00:00
【问题描述】:

我有这样的场景,我有动态生成的表。表可能包含数字字段以及文本字段。我给那个表一个特定的 CSS。我想要在表格中有文本字段的地方我给text-align: center;,在表格中有数字字段的地方我给text-align: right; 我可以使用单个Id/Class应用CSS吗?我们可以给if condition 识别表格字段是CSS 文件中的数字还是文本?

这是我动态生成的表格

<table class="alignCSS">
<thead>
    <tr>

        <th>
            Doctor Name
        </th>

        <th>
            Amount
        </th>

    </tr>
    </thead>
    <tbody>
<% foreach (var item in Model) { %>
    <tr>

        <td>
            <%: Html.DisplayFor(modelItem => item.Doctor_Name) %>
        // text field here i want to apply center alignment using CSS
        </td>

        <td>
            <%: Html.DisplayFor(modelItem => item.Amount) %>

        //number field here i want to apply right alignment using CSS
        </td>

    </tr>
<% } %>
</tbody>

</table>

【问题讨论】:

  • 显示生成表格的代码。
  • @DarinDimitrov 我已经编辑了我的问题。并添加动态生成的表格代码。

标签: css asp.net-mvc conditional


【解决方案1】:
<td style="text-align: center;">
    <%= Html.DisplayFor(modelItem => item.Doctor_Name) %>
</td>
<td style="text-align: right;">
    <%= Html.DisplayFor(modelItem => item.Amount) %>
</td>

或者如果您已经使用这些规则定义了 CSS 类:

<td class="center">
    <%= Html.DisplayFor(modelItem => item.Doctor_Name) %>
</td>
<td class="right">
    <%= Html.DisplayFor(modelItem => item.Amount) %>
</td>

【讨论】:

  • 看,我想使用外部 CSS。而且我有许多要动态生成的表。所以我不可能在每个页面中应用内部 CSS。这甚至不是可取的方式。所以我想使用一个带条件的全局外部 CSS。有没有其他办法????
【解决方案2】:

(我知道这是一个老问题......但是)您可以从控制器传递一个类的值,以便确定字段类型的逻辑可以在那里。

【讨论】:

    猜你喜欢
    • 2020-08-02
    • 2021-08-08
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 2012-09-16
    • 2021-06-27
    相关资源
    最近更新 更多