# Adding an Item in a DropDownList

## Environment

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

## Description

How can I add a new item if it doesn't exist when working with the Telerik UI for ASP.NET MVC DropDownList?

## Solution

1. Create a separate `Custom` DataSource and specify the action method for the [`.Create()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/customdatasourcetransportbuilder#createsystemactionkendomvcuifluentcustomcrudoperationbuilder) method.
2. Set the filter type for the DropDownList through the [`.Filter()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/dropdownlistbuilder#nodatatemplateidsystemstring).
3. Specify a [`NoDataTemplate`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/dropdownlistbuilder#nodatatemplatesystemstring) which will display an add confirmation dialog.
4. Inside the template, create a button and attach a handler that passes both the widget `id` and input `value`.
5. [`Sync`](https://docs.telerik.com/kendo-ui/api/data/datasource/events/sync) the data to update the records.

```Razor Index.cshtml
    @using Telerik.Examples.Mvc.Models

    @(Html.Kendo().DataSource<Location>()
        .Name("customDataSource")
        .Custom(c=>c
        .Transport(transport=>transport
            .Read(read => read.Action("GetLocations", "AddItem"))
            .Create(create => create.Action("CreateLocation", "AddItem"))
         )
        .Schema(s=>s
            .Model(m=>
            {
                m.Id("Id");
                m.Field(f=>f.Id);
                m.Field(f=>f.Name);
            })
         )
        )
    )

    @(Html.Kendo().DropDownList()
        .Name("dropDownList")
        .DataTextField("Name")
        .DataValueField("Id")
        .NoDataTemplateId("noDataTemplate")
        .Filter(FilterType.StartsWith)
        .DataSource("customDataSource")
        .HtmlAttributes(new { style = "width: 100%" })
    )
```

```C# Controller.cs
    public class AddItemController : Controller
    {
        public ActionResult AddItem()
        {
            return View("~/Views/DropDownList/AddItem.cshtml");
        }
        public ActionResult GetLocations()
        {
            IEnumerable<Location> locations = LocationsData();

            return Json(locations, JsonRequestBehavior.AllowGet);
        }
        public ActionResult CreateLocation(Location location)
        {
            return Json(new[] { location });
        }
        private List<Location> LocationsData()
        {
            return new List<Location>()
            {
                new Location() { Id = 1, Name = "London" },
                new Location() { Id = 2, Name = "Paris" },
                new Location() { Id = 3, Name = "Sofia" }
            };
        }
    }
```

```JS script.js
    <script id="noDataTemplate" type="text/x-kendo-tmpl">
        <button class="k-button" onclick="addNew('#: instance.element[0].   id #', '#: instance.filterInput.val() #')">Add new item</button>
    </script>

    <script>
        function addNew(widgetId, value) {
            var widget = $("#" + widgetId).getKendoDropDownList(); //get the reference of the dropdownlist
            var dataSource = widget.dataSource;
            if (confirm("Are you sure?")) {
                dataSource.add({ //add a new item to the datasource
                    Id: 0,
                    Name: value
                });
                dataSource.one("sync", function () {
                    widget.select(dataSource.view().length - 1);
                });
                dataSource.filter({});
                dataSource.sync(); //sync the data
            }
        };
    </script>
```

For the complete implementation of the suggested approach, refer to [this GitHub Project](https://github.com/telerik/ui-for-aspnet-core-examples/blob/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/DropDownList/AddItem.cshtml).

## More ASP.NET MVC DropDownList Resources

* [ASP.NET MVC DropDownList Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/dropdownlist/overview)

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

* [ASP.NET MVC DropDownList 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

* [Client-Side API Reference of the DropDownList for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/dropdownlist)
* [Server-Side API Reference of the DropDownList for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/dropdownlist)
* [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)
