Arun Singh
Arun Singh

Reputation: 273

Show tooltip(or title) message on every dropdown list items

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.

Upvotes: 2

Views: 6896

Answers (2)

uv_man
uv_man

Reputation: 224

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.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

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.

Upvotes: 2

Related Questions