DmitryEfimenko / TwitterBootstrapMvc

Fluent implementation of ASP.NET-MVC HTML helpers for Twitter Bootstrap.
Apache License 2.0
224 stars 79 forks source link

Disabled Item in DropDownListFor #429

Closed Steve-G-A closed 7 years ago

Steve-G-A commented 8 years ago

Hi,

Just testing your bootstrap helpers and I am trying to add a disabled item to the drop-down list. I need this for when no item is yet selected which will show an error when validated.

Could you advise how to do this?

Thanks

DmitryEfimenko commented 8 years ago

I'm not sure I understand what you are trying to achieve. Could you please provide more details? Perhaps an example?

Steve-G-A commented 8 years ago

Essentially I am after this output:

<select class="form-control" >
  <option selected disabled class="text-hide">Title</option>
  <option>A</option>
  <option>B</option>
  <option>C</option>
</select>

From http://stackoverflow.com/questions/22822427/bootstrap-select-dropdown-list-placeholder

DmitryEfimenko commented 8 years ago

There is a way to achieve what you are looking for without adding the disabled attribute. in your view model have a property with [Required] attribute:

[Required]
public string State { get; set; }

In your view use extension helper OptionLabel:

@f.FormGroup().DropDownListFor(x => x.State, Model.StatesList).OptionLabel("Select state")

Upon submission, if the first option is selected, you'll see validation error.