首页 新闻 会员 周边

asp.net mvc4绑定转换

0
悬赏园豆:10 [已解决问题] 解决于 2013-03-25 17:19

asp.net mvc4中 怎么转换列
比如
@foreach(var item in Model)
{
<tr>
<td>@item.Type</td>
<td>@item.Name</td>
</tr>
}
Type是int类型,怎么将@item.Type转换成字符类型,比如0是“老师”,1是“学生”

kuangguangxiang的主页 kuangguangxiang | 初学一级 | 园豆:36
提问于:2013-03-20 09:22
< >
分享
最佳答案
0

@(@item.Type==1 ? "老师" : "学生")

收获园豆:6
陈希章 | 老鸟四级 |园豆:2538 | 2013-03-20 20:00
其他回答(1)
0

asp.net mvc的页面引擎是支持C#代码,你可以在里面加一个if--else判断,或者switch-case的判断,当Type是0的时候,显示l老师,是1的时候显示学生。

 

另一种方法,type这里为什么不用枚举呢,如果用枚举,这里给你举个例子:

 public enum MessageStatus
    {
        [Description("未读")]
        UnRead=1,
        [Description("已读")]
        Read=2,
        [Description("退回")]
        Returned=3,
        [Description("已回复")]
        Back=4
    }

将该枚举表示的含义用特性Description标出,然后做一个枚举的扩展方法,

View Code
public static string GetDescription(this Enum value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            string description = value.ToString();
            FieldInfo fieldInfo = value.GetType().GetField(description);
            //EnumDescriptionAttribute[] attributes = (EnumDescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                description = attributes[0].Description;
            }
            else
            {
                description = value.ToString();
            }
            return description;
        }

在页面中使用枚举直接调用这个扩展方法,就可以返回该枚举的实际含义。

收获园豆:4
小李北漂 | 园豆:176 (初学一级) | 2013-03-20 10:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册