asp.net-mvc-3 - 我如何改进 Razor View 中的代码以根据文本内容更改文本的颜色

标签 asp.net-mvc-3 razor

我的 razor View 中有以下代码,可以根据文本内容更改文本的颜色:-

string status = ViewData[vlr.LabTestID.ToString()].ToString();

              if (status.ToUpper().StartsWith("Erro".ToUpper()))
                {
        <td style="color: #b30000">
       @status
        </td>
                }
             else   if (status.ToUpper().StartsWith("With".ToUpper()))
                {
        <td style="color: #6b9e52">
        @status
        </td>}
              else  if (status.ToUpper().StartsWith("Below".ToUpper()))
                {
        <td style="color: #b30000">
        @status
        </td>}
             else   if (status.ToUpper().StartsWith("Above".ToUpper()))
                {
        <td style="color: #b30000">
        @status
        </td>}
              else  if (status.ToUpper().StartsWith("Cannot".ToUpper()))
                {
        <td style="color: #5c87b2">
        @status
        </td>}
             }

但是有没有一种方法可以使用比上述方法更可靠、更简单的方法来执行相同的功能? BR

最佳答案

您可以为每种可能性定义一个 CSS 样式,然后让它完成工作。

Razor

<td class="@status.ToUpper()">
@status
</td>

CSS

.ERRO
{
    color: #6b9e52;
}

.WITH
{
    color: #b30000;
}

关于asp.net-mvc-3 - 我如何改进 Razor View 中的代码以根据文本内容更改文本的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10777852/

相关文章:

asp.net - Application_MapRequestHandler 有时真的很慢

asp.net-mvc - 是否可以使用 Razor 创建通用的 @helper 方法?

ASP.NET MVC5 Bootstrap CSS 搞乱了导航?

asp.net-mvc - C# razor url 参数 View

asp.net-mvc - Html.CheckBox 指定类

c# - 如何为 Razor View 创建默认的 AjaxOptions?

c# - 使用 Visual Studio 内置的 ASP.NET MVC 模板

c# - devexpress元素的样式

javascript - 将对象从 cshtml View 传递到 JQuery

c# - System.Collections.Generic.List 的计数