我的 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/