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
  • 57,247
  • 24
  • 95
  • 149
The Light
  • 25,391
  • 62
  • 170
  • 255

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,188
  • 13
  • 25