How do you make an 'enum' that has data tied to it?

前端 未结 6 1908
深忆病人
深忆病人 2021-01-23 02:28

I have a Vote class and one of the properties it can have is a vote type. Such as unanimous, a 3/4 vote, a simply majority, etc. Each type needs to have a string associated with

6条回答
  •  感情败类
    2021-01-23 02:53

    Since you have the type and description I'll better suggest you to create a class that wraps up both instead of enum. The advantage is you can reduce more work and it's very flexible.

    public class VoteType
    {
       public string Name{ get; set; }
       public string Description{ get; set; }
    }
    

    Now your Vote class will have reference to this VoteType.

    public class Vote
    {
       ...
       public VoteType Type{ get; set; }
    }
    

    In your VoteViewModel you better have a class that contains all the VoteTypes.

    public class VoteViewModel
    {
       ...
       public IEnumerable VoteTypes{ get; set; }
    }
    

    Now you can easily bind the VoteTypes in a dropdownlist.

    @model VoteViewModel
    
    @Html.DropDiwnListFor(m => m.VoteTypes,...)
    

提交回复
热议问题