【发布时间】:2016-08-02 13:42:09
【问题描述】:
我的 MVC 项目中有一个问题类别枚举:
public enum QuestionCategory
{
[Display(Name = "Enum_Category_Overall_Display", ResourceType = typeof(ResQuestion))]
Overall = 0,
[Display(Name = "Enum_Category_Service_Display", ResourceType = typeof(ResQuestion))]
Service = 1,
[Display(Name = "Enum_Category_Facilities_Display", ResourceType = typeof(ResQuestion))]
Facilities = 2,
[Display(Name = "Enum_Category_Team_Display", ResourceType = typeof(ResQuestion))]
Team = 3
}
而且我需要根据 Enum 的值在 View 中设置图表颜色。
现在我在视图中进行切换:
string color = "";
switch (answer.QuestionCategory)
{
case Core.Data.Models.QuestionCategory.Overall: color = "#10c469"; break;
case Core.Data.Models.QuestionCategory.Service: color = "#f9c851"; break;
case Core.Data.Models.QuestionCategory.Facilities: color = "#188ae2"; break;
case Core.Data.Models.QuestionCategory.Team: color = "#5b69bc"; break;
}
我在图表中应用颜色:
<round-progress max="5" current="@answer.Numeric" color="@color" semi="isSemi" rounded="rounded" clockwise="clockwise" responsive="responsive"></round-progress>
问题是我需要基于另一个视图中枚举值的颜色,我不想重复代码(DRY)。
【问题讨论】:
-
请不要把这个处理放在视图中,它不整洁。为了解决可重用性问题,请在后端创建一个辅助方法,以便您可以重复调用它并将处理后的值分配给模型,并将该视图绑定到该模型中
-
您好,谢谢!但在您看来,样式决定不应该在表示层中做出,在这种情况下,视图?
-
是的,即使是样式决定,值仍然是硬编码的,我可以在后端完成
标签: c# css asp.net-mvc enums