I've got a select tag in my view that's populated with items from an enum in my class. I'm using the selected value to show/hide elements on my page. This was a set of radio buttons before but as the number of options grew, I wanted to put it in a nicer form control than a bunch of buttons. To test functionality I'm just trying to display the selected value before I re-implement the show/hide.
My model
[Serializable]
public class AddUser
{
[Required]
[Display(Name = "User Type")]
public UserTypes UserType { get; set; }
public enum UserTypes
{
[Display(Name = "Standard")] Standard,
[Display(Name = "Standard + Email")] StandardAndEmail,
[Display(Name = "Standard + Email + Phone")] StandardAndEmailAndPhone,
[Display(Name = "Email Only")] EmailOnly,
[Display(Name = "Phone Only")] PhoneOnly
}
}
Tag from view View
<select id="userType" class="form-control" asp-for="UserType" asp-items="Html.GetEnumSelectList<AddUser.UserTypes>()" onchange="userTypeSelect()">
<option selected="selected" value="">Select</option>
</select>
<span asp-validation-for="UserType" class="text-danger"></span>
Javascript
<script type="text/javascript">
function userTypeSelect() {
var userType = document.getElementById("userType").value;
console.log('User Type: ' + userType)
}
</script>
Here is the resulting HTML
Desired result would be the rendered HTML options looking like this:
<option value="Standard">Standard</option>
Since I plan on using the value in Javascript, having access to the name rather than just an index would be ideal for code readability - especially since this isn't going to a database. I've been at this all morning trying to figure out how to get this to work.
Is this possible? If so, is it still better to just use the ID anyway?