0

Is there a way to show all options in the Select created by the helper Html.DropDownListFor ?

I was searching for the event triggered when you click on the select to show all options to manually trigger but i did not find it.

@Html.DropDownListFor(
    model => model.Value, 
    Model.Values, 
    htmlAttributes: new { @class = "form-control", @style = "width:150px;" })
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Soufien Hajji
  • 477
  • 1
  • 8
  • 24
  • Can you explain what you mean by "show all options" - as it is, your code creates a `select` drop down - none of the items are "hidden" but the drop down does have a maximum height which means some values may need to be scrolled to, but they're still "shown". – freedomn-m Apr 02 '20 at 12:54
  • Or do you mean to "open" the `select` either on page load or via some other code? That's not possible. – freedomn-m Apr 02 '20 at 12:55
  • 1
    I want the default state of the select would be as if i click once on it. when i click on it it "shows me all the options", i want the that to be as a default – Soufien Hajji Apr 02 '20 at 12:57
  • yes the word open is more correct – Soufien Hajji Apr 02 '20 at 12:58
  • Unfortunately, it appears that's not possible: https://stackoverflow.com/questions/249192/how-can-you-programmatically-tell-an-html-select-to-drop-down-for-example-due (amongst many other similar questions, more specifically about coding it, but it's essentially the same thing) – freedomn-m Apr 02 '20 at 13:00
  • I've found something with the size attribute. WHen defining the size of the size equal to the number of options it opens all the options by default. – Soufien Hajji Apr 02 '20 at 13:02
  • Yes, that's a select list - it doesn't operate the same was a select drop down (eg doesn't drop over the top of other content / does move around all your other content). If that works for you, then go for it. Example: http://jsfiddle.net/uzcds5v6 to get this in Razor, just add `htmlAttributes: new { size: "5" ...` (or however many lines you want) – freedomn-m Apr 02 '20 at 13:05

1 Answers1

0

Try using a ListBoxFor

@Html.ListBoxFor(model=>model.Id,model.Values as SelectList)
zybroxz
  • 703
  • 1
  • 7
  • 18