选中的单选按钮枚举?

时间:2013-08-25 19:39:57

标签: c# asp.net-mvc-4

您好我有一个名为activitylevel的枚举模型以及一个名为activityselected的属性,我有一个radiobuttons表绑定到activityselected属性,当选择radiobutton时我想将相应的枚举传递给activityselected属性,在我看来它说“ActivityLevel在当前上下文中不存在”?只是想知道我出错了还是有更好的办法吗? 谢谢你的帮助!!

型号:

  public class UserStats
    {
        public enum ActivityLevel
        {
            Sedentary,
            LightActivity,
            Moderate,
            Active,
            Extra
        }

        public Enum ActivitySelected { get; set; }

        [Key]
        public int UserID { get; set; }

查看:

      <table>
       <tr>
    <td>
        @Html.RadioButtonFor(model => model.ActivitySelected, ActivityLevel.Sedentary) Sedentary
    </td>
</tr>
<tr>
    <td>
        @Html.RadioButtonFor(model => model.ActivitySelected,ActivityLevel.LightActivity) Light Activity
    </td>
</tr>    
<tr>
    <td>
        @Html.RadioButtonFor(model => model.ActivitySelected, ActivityLevel.Moderate) Moderate
    </td>
</tr>
            <tr>
         <td>
        @Html.RadioButtonFor(model => model.ActivitySelected,ActivityLevel.Active) Active
    </td>
</tr>    
<tr>
    <td>
        @Html.RadioButtonFor(model => model.ActivitySelected, ActivityLevel.Extra) Extra
    </td>
</tr>
    </table>

1 个答案:

答案 0 :(得分:1)

枚举是在UserStats类中声明的,因此您必须使用UserStats.ActivityLevel.XXX引用它或将枚举移出类(这是我推荐的)

另外,我会通过真实的类型名称声明枚举成员,而不是枚举类型:

public enum ActivityLevel
{
   Sedentary,
   LightActivity,
   Moderate,
   Active,
   Extra
}

public class UserStats
{

    public ActivityLevel ActivitySelected { get; set; }

    [Key]
    public int UserID { get; set; }