0

Hi I have a model that contains an enum called activitylevel and also a property called activityselected, I have a table of radiobuttons that are bound to the activityselected property, when a radiobutton is select I want to pass the corresponding enum to the activityselected property, in我的观点是“当前上下文中不存在 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>
4

1 回答 1

1

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

此外,我将通过其真实类型名称声明枚举成员,而不是类型 Enum:

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

public class UserStats
{

    public ActivityLevel ActivitySelected { get; set; }

    [Key]
    public int UserID { get; set; }
于 2013-08-25T21:37:37.113 回答