Aspnetcore: [SelectListItem] Missing hidden

Created on 16 Sep 2019  路  3Comments  路  Source: dotnet/aspnetcore

I want to have a select with the first option hidden, like this one:
image

I can generate it with this code:

<select name="mySelect" id="mySelect" class="form-control">
    <option value="" disabled selected hidden>Choose</option>
    <option value="0">My Option 1</option>
    <option value="1">My Option 2</option>
    <option value="2">My Option 3</option>
</select>

But how can I get it to work using @Html.DropDownList?

c# @{ var myOptions= SelectListHelper.myOptions().ToList(); myOptions.Insert(0 , (new SelectListItem { Text = "Choose", Value = "", Disabled = true, Selected = true, Hidden = true })); } @Html.DropDownList("mySelect", myOptions, new { @class = "form-control" })

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/hidden

area-mvc enhancement

Most helpful comment

It's not possible with Html.DropDownList. However @eduardogoncalves you could make this work with the select TagHelper:

@{
    var myOptions = new[] 
    { 
        new SelectListItem { Text = "Choose 1", Value = "", },
        new SelectListItem { Text = "Choose 2", Value = "", }, 
        new SelectListItem { Text = "Choose 3", Value = "", } 
    };
}


<select asp-items="@myOptions">
    <option value="" disabled selected hidden>Choose</option>
</select>

All 3 comments

Thanks for contacting us, @eduardogoncalves.
@NTaylorMullen isn't this possible today? If it is, can you please suggest how, and close this ? Thanks!

It's not possible with Html.DropDownList. However @eduardogoncalves you could make this work with the select TagHelper:

@{
    var myOptions = new[] 
    { 
        new SelectListItem { Text = "Choose 1", Value = "", },
        new SelectListItem { Text = "Choose 2", Value = "", }, 
        new SelectListItem { Text = "Choose 3", Value = "", } 
    };
}


<select asp-items="@myOptions">
    <option value="" disabled selected hidden>Choose</option>
</select>

Thanks @NTaylorMullen!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

farhadibehnam picture farhadibehnam  路  3Comments

ermithun picture ermithun  路  3Comments

guardrex picture guardrex  路  3Comments

UweKeim picture UweKeim  路  3Comments

aurokk picture aurokk  路  3Comments