0

So I have the following enum :

public enum Type
{
A ,
S
}

At the moment I have the following code in my view which uses this enum:

<div class="editor-label">
@Html.Label("Type")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Type, new SelectList(Enum.GetValues(typeof(Project.Domain.POCO.Type))))
@Html.ValidationMessageFor(model => model.Type)
</div>

However, the original idea was to use a checkbox and not a dropdownlist, this was just a temporarily solution. Whenever I try using @Html.CheckBoxFor it expects a boolean type, is there any way to work around this and keep using my enumtype instead of creating a boolean (since the enum is used in multiple classes and I'd have to make a lot of changes if I were to change it into a boolean)

1
  • 1
    Make a YourPageViewModel with a bool AmIA { get; set; }? Commented Apr 26, 2013 at 9:22

1 Answer 1

3

You can parse your enum to a boolean since your enum types are also int values:

A = 0, S = 1..

since you only have two values you always have 0 or 1. This can be parsed into a bool. Note that this is a very dirty solution.

when you use:

Boolean.Parse((int)model.Type)

you'll get a bool. A = false, S = true

However I wouldn't recommend using this approach

Sign up to request clarification or add additional context in comments.

2 Comments

Allright thanks a lot! Why is is considered a dirty solution though? (I'm new to asp.net programming)
Well, it's not programming safe because when you add more types to this enum you won't be left with only 0 or 1, but also 2 and so on. if this answer helped you, you might consider upvoting it, or even accepting it as an answer (or both)

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.