我正在用mvc3开发一个应用程序...... 我正忙着处理ENUM。
我做了一个枚举类:
Hobby.cs
public enum Hobby
{
[Display(Name = "Dancing")]
Dancing = 1
,
[Display(Name = "Singing")]
Singing = 2
,
[Display(Name = "Painting")]
Painting = 3
,
[Display(Name = "Cooking")]
Cooking = 4
,
[Display(Name = "Photography")]
Photography = 5
}
我在另一个名为HobbyDetail.cs的类中使用了Hobby类作为类型
public class HobbyDetail
{
[Key]
public virtual int HobbyDetailId { get; set; }
[EnumDataType(typeof(Hobby), ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "HobbyRequired")]
[Display(Name = "lblHobby", ResourceType = typeof(Resources))]
public virtual Hobby Hobby { get; set; }
public virtual string StudyMedium { get; set; }
public virtual decimal Fees { get; set; }
public virtual char ForWhom { get; set; }
public virtual Boolean InclusiveStudyMaterial { get; set; }
public virtual int AgeFrom { get; set; }
public virtual int AgeTo { get; set; }
public virtual string HobbyInfo { get; set; }
}
然后我进一步在另一个类
中创建了上述类的对象将Person.cs命名为
public virtual HobbyDetail Hobby { get; set; }
我在控制器中插入了以下代码:
public viewResult(Person person)
{
PersonalProfileService personalProfileService = new PersonalProfileService();
person.Hobby.Hobby = Hobby.Dancing;
personalProfileService.SaveBasicProfileWithHobby(person);
}
我正在使用NHibernate和SaveBasicProfileWithHobby()是服务类的方法..它们工作得很好......
我能够将静态值存储在数据库中,但是我无法将动态值保存到我的数据库中,用户从枚举帮助创建的下拉列表中选择了该值。 建议放置在控制器内部的行代码,这样可以帮助我将动态枚举值存储在数据库中。 请..
答案 0 :(得分:1)
HobbyDetail.cs:
public short HobbyId {get; set;}
并获得如下值:
public Hobby Hobby
{
get
{
return (Hobby)HobbyId;
}
set
{
HobbyId = (byte)Hobby;
}
}
你的枚举可以是这样的:
public enum Hobby : byte
{
Dancing = 1,
Singing = 2
}
希望它有所帮助...