关于MVC使用枚举,把数据库字段转换成自己想要显示的值

weixin_40872356 2017-11-04 04:19:40
比如数据库取出来数据,然后有个status字段,int类型的,我想用枚举实现:

比如字段值是1,前台显示“正常”,这个用枚举怎么实现,谢谢大家了
...全文
448 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_40872356 2017-11-07
  • 打赏
  • 举报
回复
引用 7 楼 qq_29295613 的回复:
public enum ShopingOrderState { NoPay = 0, } public static ShopingOrderState State(this ShoppingOrders order) { return (ShopingOrderState)order.State; } public static string StateToString(this ShoppingOrders order) { var State = order.State(); var result = string.Empty; switch (State) { case ShopingOrderState.NoPay : result = "你猜"; break; } } MVC的 可以直接.StateToString() 出来就是文字了 你要的是这个吗?
不是呀,我就是从数据库取出来的List集合然后异步传到前台json字符串,要是就一条数据就没那么费事了
swl82560397pq 2017-11-07
  • 打赏
  • 举报
回复
为什么csdn积分兑换不了,现在还这么多广告
insus 2017-11-07
  • 打赏
  • 举报
回复
正怒月神 版主 2017-11-06
  • 打赏
  • 举报
回复
你的意思,其实是使用 枚举 Enum + 标签属性 Description
//是 否
public enum IsOrNot
{
//否
[Description("No")]
No = 0,

//是
[Description("Yes")]
Yes = 1
}


至于获取 Description的方法,参考
http://www.cnblogs.com/xinxinzhihuo/p/5978103.html

  • 打赏
  • 举报
回复
#1楼的三元运算符形式就可以的,不用那么麻烦,就单单显示两个状态而已。
weixin_40872356 2017-11-06
  • 打赏
  • 举报
回复
实现方法其实有很多,我在Controller里面foreach判断一下每个值都改一下也可以,但是那样我感觉如果数据量稍微大点的话就会影响速度,其实正常设计个字典表,表关联都行,我基础不咋好,枚举不知道咋实现,没有表关联的情况下,看看能不能效率高一些,不行也没招了
qq_29295613 2017-11-06
  • 打赏
  • 举报
回复
public enum ShopingOrderState { NoPay = 0, } public static ShopingOrderState State(this ShoppingOrders order) { return (ShopingOrderState)order.State; } public static string StateToString(this ShoppingOrders order) { var State = order.State(); var result = string.Empty; switch (State) { case ShopingOrderState.NoPay : result = "你猜"; break; } } MVC的 可以直接.StateToString() 出来就是文字了 你要的是这个吗?
正怒月神 版主 2017-11-06
  • 打赏
  • 举报
回复
引用 5 楼 weixin_40872356 的回复:
这例子我看了下,有点蒙,我现在是list集合,然后传到前台json字符串,现在json数据里面全都是int类型的状态(1:正常,0:停用),该怎么弄,求指教
那主要实现一下根据值类型获取中文描述的功能就可以了。
static void Main(string[] args)
        {
            string json = "{\"id\":1,\"name\":\"hello\",\"state\":0}";
            var q = JsonConvert.DeserializeObject<dynamic>(json);
            var r = ((IsOrNot)q.state).GetDescription();
            Console.WriteLine(r);       //No

            string json1 = "{\"id\":1,\"name\":\"hello\",\"state\":1}";
            var q1 = JsonConvert.DeserializeObject<dynamic>(json1);
            var r1 = ((IsOrNot)q1.state).GetDescription();
            Console.WriteLine(r1);      //Yes

            Console.ReadLine();
        }

        //是 否
        public enum IsOrNot
        {
            //否
            [Description("No")]
            No = 0,

            //是
            [Description("Yes")]
            Yes = 1,

        }

        public static string GetDescription(this Enum value, Boolean nameInstead = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }

            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

            if (attribute == null && nameInstead == true)
            {
                return name;
            }
            return attribute == null ? null : attribute.Description;
        } 
weixin_40872356 2017-11-06
  • 打赏
  • 举报
回复
这例子我看了下,有点蒙,我现在是list集合,然后传到前台json字符串,现在json数据里面全都是int类型的状态(1:正常,0:停用),该怎么弄,求指教
snlixing 2017-11-04
  • 打赏
  • 举报
回复
为啥要用枚举呢,直接绑定不行吗、 <%# Eval("status").ToString() == "1" ? "正常" : "异常" %>

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧