Add attribute to select list option

后端 未结 3 1989
青春惊慌失措
青春惊慌失措 2020-12-09 17:50

I have a list of items in a drop down list within a Razor view. In the database each item has 3 values associated with it - the database id, short name (for display), and lo

3条回答
  •  孤街浪徒
    2020-12-09 18:01

    Inside the controller action that is supposed to receive the form submit you could use the id of the selected value to query your database in order to fetch the long display name and do whatever you was intending to do with it.

    The DropDownListFor helper doesn't support adding HTML5 data-* attributes to the options but even if it did they will not be sent as part of a standard form submission. You will have to use javascript to send them to the server using another technique (hidden fields, AJAX, query string parameters, ...).

    But if form some reason you need additional attributes on the option tag you could always write a custom helper.

提交回复
热议问题