# Creating a DropDownList Command Column in the Grid

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2022.2.802 version</td>
 </tr>
</table>

## Description

How can I implement an always visible DropDownList command template for the Telerik UI for ASP.NET MVC Grid?

## Solution

To achieve the desired scenario:

1. Specify a DropDownList command template by using the [`.Template()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/gridcustomactioncommandbuilder#templatesystemstring) configuration option for a custom command.
2. Within the template, specify an empty `<input>` tag and decorate it with a common class that will be used for rendering a DropDownList for each rows respectively.
3. Create a common `DataSource` for the DropDownList.
4. Traverse through each records by handling the [`DataBound`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/grideventbuilder#databoundsystemstring) event of the Grid. To initialize the DropDownlists, use the previously decorated common class for the custom command input.
5. Handle each of the command options by subscribing to the [`Change`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/dropdownlisteventbuilder#changesystemstring) event of the DropDownLists.

```Razor HtmlHelper
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.ProductName);
            columns.Bound(p => p.UnitPrice).Width(100);
            columns.Bound(p => p.UnitsInStock).Width(100);
            columns.Bound(p => p.Discontinued).Width(100);
            columns.Command(command => { command.Custom("myCommand").Template("<input class='dropDownTemplate'/>"); }).Width(172);
        })
        .ToolBar(toolbar =>toolbar.Create())
        .Editable(editable => editable.Mode(GridEditMode.InLine))
        .Pageable()
        .Sortable()
        .Events(e => e.DataBound("onDataBound"))
        .Scrollable()
        .HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Model(model => model.Id(p => p.ProductID))
            .Create(update => update.Action("EditingInline_Create", "Grid"))
            .Read(read => read.Action("EditingInline_Read", "Grid"))
            .Update(update => update.Action("EditingInline_Update", "Grid"))
            .Destroy(update => update.Action("EditingInline_Destroy", "Grid"))
        )
    )
```

```JS script.js
<script type="text/javascript">
    var ddlDataSource = [ // Create a common DataSource.
            {
                value: 1,
                displayValue: "Command1"
            },
            {
                value: 2,
                displayValue: "Command2"
            }
    ];
    function onDataBound(e){
        var grid = e.sender; // Get the Grid's reference.

        var items = e.sender.items(); // Get the Grid's data items.

        items.each(function(e) { // Traverse through each of the items.
            var dataItem = grid.dataItem(this); 
            var ddt = $(this).find('.dropDownTemplate'); // Retrieve the current input element.

            $(ddt).kendoDropDownList({ // Initialize a DropDownList.
                value: dataItem.value,
                change: onChange,
                dataSource: ddlDataSource,
                optionLabel: "Select a command...",
                dataTextField: "displayValue",
                dataValueField: "value"
            });
        });
    }
    function onChange(e){ 
        var command = e.sender.text(); // Get the current text.

        switch(command){
            case "Command1":
                alert(command+" "+"is selected.");
                break;
            case "Command2":
                alert(command+" "+"is selected.")
                break;          
            default:
                break;  
        }
    }
    </script>
```

For the complete implementation of the suggested approach, refer to the [Telerik REPL example on implementing a DropDownList command column in the Grid](https://netcorerepl.telerik.com/ccaZvdPx13soCW7O04).

## More ASP.NET MVC Grid Resources

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)

* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)

* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Telerik REPL: Implementing a DropDownList Command Column in the Grid](https://netcorerepl.telerik.com/ccaZvdPx13soCW7O04)
* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
