In my view I have a enumdropdownlist (a new feature in Asp.Net MVC 5.1).
@Html.EnumDropDownListFor(m => m.SelectedLicense,new { @class="form-control"})
If I execute the above code I get dropdownlist for my following enum.
public enum LicenseTypes
{
Trial = 0,
Paid = 1
}
but by default I want my dropdownlist to have a value(custom text)
and this is what I tried
@Html.EnumDropDownListFor(m => m.SelectedLicense,"Select a license" ,new { @class="form-control"})
but now the problem is when i run it, my dropdownlist looks like this
So, the default text I want to show doesn't appear by default.
If a user selects "select a license" and tries to submit the form, it does show an error saying "select a license" but it doesn't show as default text.
Something i need to change?
Ps: The image is the screenshot of the page when it loads. By default it'll show Trial as selected option.
Best Answer
Try to change the
Index
ofLicenseTypes
start from1
not0
like below:Then you can use
Range attribute
to validate the selected license type like below:Finally, in your view: