[英]Style based on Enum value
I have an enum as follows: 我有一个枚举如下:
enum ProgressStatus{
Approved,
Unapproved,
InProcess
}
I have an Project
object that use this class. 我有一个使用此类的Project
对象。 Based on the enum values I need to show their values as Green, Yellow or Blue in while styling. 基于枚举值,我需要在样式设置时将它们的值显示为绿色,黄色或蓝色。
<ol class="breadcrumb">
@if (Model.ProgressStatus == ProgressStatus.Approved)
{
<li style="color:red;"><label>Vaziyet Planı</label></li>
}
</ol>
This is how i try to do it but it get cumbersome and clumsy. 这就是我尝试这样做的方法,但是它变得笨拙而笨拙。
What is the correct way to get this done? 完成这项工作的正确方法是什么?
Put this in your view: 将此视为您的观点:
@{
var labelColours = new Dictionary<ProgressStatus, string>()
{
{ ProgressStatus.Approved, "green" },
{ ProgressStatus.Unapproved, "yellow" },
{ ProgressStatus.InProcess, "blue" }
};
}
Then use this in your label: 然后在标签中使用:
<li style="color: @labelColours[Model.ProgressStatus];">
with your enum, you can use a short switch to set the color: 对于您的枚举,可以使用一个短开关来设置颜色:
@{
string color;
switch(Model.ProgressStatus)
{
case ProgressStatus.Approved : color = "green"; break;
case ProgressStatus.Unapproved : color = "yellow"; break;
//...
}
}
then you only need the one element 那你只需要一个元素
<li style="color:@color;"><label>Vaziyet Planı</label></li>
This is just a different take on Stijn's answer really. 实际上,这只是对Stijn答案的不同看法。 I like his better. 我喜欢他更好。
将此逻辑放在控制器中,并将其解析为绑定到视图的单个模型属性。
<li class='@Model.FinalStyle'><label>@Model.Text</label></li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.