2

I’m using a custom dropdownlist and binding elements dynamically, but here I want to show a tooltip message on every items of dropdown list.

View Code:

  @Html.DropDownListFor(m => m.Industry,Model.IndustryList, "All", new { @style = "width:258px;", @class = "drpDown" }) 

Controller code:

IEnumerable<SelectListItem> IndustryList= EntityModel<T>.Where(m => m.t == “something”).ToList().Select(c => new SelectListItem { Text = t.Name, Value = t.Value);

So let me know is there any way to set title on every option items within select tag.

tereško
  • 58,060
  • 25
  • 98
  • 150
Arun Singh
  • 263
  • 8
  • 19

2 Answers2

2

No, the standard Html.DropDownListFor helper doesn't support setting any attributes on the <option> tags except the standard ones (value and selected). You could write a custom helper to achieve that. You may check this example as well as this one.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

I would also probably go for Darin's answer, you need a custom drop down list.

A quick (and dirty) alternative might be to loop through manually and create your select list and set the title value on the option.

On your controller, maybe populate a view model as such (I'm just using an anonymous object as an example):

var SelectListViewModel = new[] {
            new {
                Text = "One",
                Title = "I'm One Title",
                Value = "1"
            },
            new {
                Text = "Two",
                Title = "I'm Two Title",
                Value = "2"
            }

        };

and in your view:

<select>
    @foreach (var item in SelectListViewModel)
    {
        <option title="@item.Title" value="@item.Value">@item.Text</option>
    }
</select>

Not the cleanest, but hopefully might be helpful for someone.

uv_man
  • 224
  • 1
  • 3