BKM BKM - 2 months ago 4
ASP.NET (C#) Question

The default '---Select--' is not coming in the razor drop-down while loading that drop-down on button click

The value is getting populated in the drop-down but the default value

is not coming on top of drop-down populated list.

@Html.DropDownListFor(model => model.HoursVal, new SelectList(Enumerable.Range(0, 24)), "--select--")


I think your issue is that HoursVal is an integer so it's default value is 0 and since your select list is new SelectList(Enumerable.Range(0, 24)) which has 0 value as well it will always be selected in case that HoursVal property is not set for a specific value. If for example HoursVal was int? or a string and you won't set is to any value (leave it null) your dropdown will select the --select-- option.