How do I make my Dropdownlistfor equal to Select

Asked

Viewed 347 times

0

How do I leave my Dropdownlistfor, like this select where the first field cannot 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"))

2 answers

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"))
  • Pretty sure that doesn’t work with SelectList

  • It’s not worked

  • Sorry, I didn’t pay attention. It doesn’t work with selectList. It changes to Selectlistitem like LINQ, he replied. That’s right.

2


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

For this to work, you will need to change the creation of the drop down list a little. Instead of using an object of the 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"))
  • I tried gave this error: "System.Web.Mvc.Selectlistitem' does not contain a definition for 'Disabled'"

  • You’re actually using ASP.NET MVC 5?

  • Well I do not know right he has the property Selected, Value, Text, but now I changed and this working I am doing in HTML same

  • If you don’t know you shouldn’t have used the mvc 5 tag. I’ll see how to do in previous versions.

  • No need ta ok, I think I selected the tag without paying attention

Browser other questions tagged

You are not signed in. Login or sign up in order to post.