2

I have the below code in MVC, Razor:

- @Html.DropDownListFor(model => model.SelectedEvent,
                                 new SelectList(Model.Events, "Id", "Name"))

I'd like to add a new attribute for each option in the select called "description" which should be bound to the Event.Description.

How to achieve this?

I'd like to avoid creating a separate Html Helper. Would that be possible?

tereško
  • 58,060
  • 25
  • 98
  • 150
The Light
  • 26,341
  • 62
  • 176
  • 258

1 Answers1

0

Unfortunately, this is not supported by current DropDownListFor implementation. There is an overload that takes a IDictionary<string, Object> of html attributes, but they are bound to the select element, not to its items, as you can verify in the MSDN documentation. I don't think you will be able to do what you need here without implementing a custom helper.

Check out a couple of custom helpers that implement a similar behaviour in the answers to this question.

Community
  • 1
  • 1
rla4
  • 1,228
  • 13
  • 25