Search results

Getting started with ASP.NET MVC DropDownButton control

25 Oct 2021 / 1 minute to read

This section briefly explains about how to include a simple DropDownButton in your ASP.NET MVC application. You can refer ASP.NET MVC Getting Started documentation page for introduction part of the system requirements and configure the common specifications.

Add DropDownButton to the project

We are going to render DropDownButton component in Index.cshtml page.

razor
Copied to clipboard
@Html.EJS().DropDownButton("element").Content("Edit").Items((IEnumerable<object>)ViewBag.items).Render()

ViewBag.items variable is used for bounding the items property in view page.

Bind dataSource

Populate the action items in SplitButton by using the items property. Here, the JSON values are passed to the DropDownButton component are generated in default.cs and assigned to ViewBag variable.

Copied to clipboard
public ActionResult Index()
{
    List<object> items = new List<object>();
    items.Add(new
    {
        text = "Cut"
    });
    items.Add(new
    {
        text = "Copy"
    });
    items.Add(new
    {
        text = "Paste"
    });
    ViewBag.items = items;
    return View();
}

Output be like the below.

DropDownButton Sample

Run the application

After successful compilation of your application, simply press F5 to run the application.

The following example shows a basic DropDownButton.

razor
default.cs
Copied to clipboard
@Html.EJS().DropDownButton("element").Content("Edit").Items((IEnumerable<object>)ViewBag.items).Render()
Copied to clipboard
public ActionResult Index()
{
    List<object> items = new List<object>();
    items.Add(new
    {
        text = "Cut"
    });
    items.Add(new
    {
        text = "Copy"
    });
    items.Add(new
    {
        text = "Paste"
    });
    ViewBag.items = items;
    return View();
}

See Also