1

I have enum of 3 values. I want to bind the enum values to a @Html.CheckBoxexcept the first one. How this can be acheived?

My enum-

public enum EType
{
    [EnumMember]
    UNKNOWN = 0,
    [EnumMember]
    Value1 = 1,
    [EnumMember]
    Value2 = 2
}

This is my ViewBag in controller which contains the enum values-

ViewBag.Enums = from Enum e in Enum.GetValues(typeof(EType))
                select new SelectListItem { Value = Convert.ToInt32(e).ToString(), Text =((EType)e).ToString() };

This is my Html-

<td>
@{
IEnumerable<SelectListItem> Enums = ViewBag.Enums;
foreach (var item in Enums)
 {                                                                   
  @Html.CheckBox(item.Text, false, new { item.Value }) 
  <label>@item.Text</label><br />
 }
}

1 Answer 1

3

In View, try this

var enumList = Enum.GetValues(typeof(EType)).Cast<EType>().Skip(1);

@foreach (var optVal in enumList)
{ 
<label>
    @Html.CheckBox(optVal.ToString(), false, new { value = Convert.ToInt32(optVal).ToString()})
    @optVal

</label>
}
Sign up to request clarification or add additional context in comments.

1 Comment

Cast<EType>().Skip(1) helped me. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.