How do I leave my DropDownListFor equal to Select

0

How do I get my DropDownListFor, like this select where the first field can not be selected.

<select>
  <option value="0" selected disabled>Selecionar</option>
  <option value="1">SEMANAL</option>
  <option value="2">MENSAL</option>
  <option value="3">ANUAL</option>
</select>

...

@Html.DropDownListFor(s => s.MeetingFrequency, new SelectList(new List<Object> {
  new { value = "0", text ="Selecionar"},
  new { value = "SEMANAL", text = "Semanal" },
  new { value = "MENSAL", text = "Mensal" },
  new { value = "ANUAL", text = "Anual" }
}, "value", "text"))
    
asked by anonymous 20.07.2017 / 15:30

2 answers

2

You can set the value of the property Disabled of SelectListItem to true .

For this to work, you will need to slightly change the creation of the drop down list. Instead of using an object of type SelectList , use a list of SelectListItem .

Example:

@Html.DropDownListFor(s => s.MeetingFrequency, new List<SelectListItem> 
{
    new SelectListItem { Value = "0", Text = "Selecionar", Disabled = true},
    new SelectListItem { Value = "SEMANAL", Text = "Semanal" },
    new SelectListItem { Value = "MENSAL", Text = "Mensal" },
    new SelectListItem { Value = "ANUAL", Text = "Anual" }
}, "value", "text"))
    
20.07.2017 / 15:51
2
@Html.DropDownListFor(s => s.MeetingFrequency, new SelectList(new List<Object> {
  new { value = "0", text ="Selecionar",disabled = "disabled"},
  new { value = "SEMANAL", text = "Semanal" },
  new { value = "MENSAL", text = "Mensal" },
  new { value = "ANUAL", text = "Anual" }
}, "value", "text"))
    
20.07.2017 / 15:39