Articles in this section
Category / Section

How to set multiselect dropdown in ASP.NET MVC Grid?

4 mins read

In certain cases, you may like to set the column edit type as multi select dropdown with checkboxes in ASP.NET MVC Grid. You can achieve this by using the EditTemplate property of the Grid columns. The EditTemplate property is used to display a custom editor (any other control) for a column.

In the following code example, a Grid is rendered with the batch editing enabled.

  1. Render the grid.

MVC

    @(Html.EJ().Grid<object>("Grid")
    .Datasource(((IEnumerable<object>)ViewBag.dataSource))
         .AllowPaging()
             .ToolbarSettings(toolbar =>
                             {
                                 toolbar.ShowToolbar().ToolbarItems(items =>
                                 {
                                     items.AddTool(ToolBarItems.Add);
                                     items.AddTool(ToolBarItems.Edit);
                                     items.AddTool(ToolBarItems.Delete);
                                     items.AddTool(ToolBarItems.Update);
                                     items.AddTool(ToolBarItems.Cancel);
                                 });
                             })
                            .EditSettings(edit => edit.AllowEditing().AllowAdding().AllowDeleting())
        .Columns(col =>
        {
            col.Field("CustomerID").HeaderText("Customer ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Add();
            col.Field("CustomerName").HeaderText("Customer Name").Add();
            col.Field("OrderID").HeaderText("Order ID").Add();
            col.Field("OrderList").HeaderText("Order List").EditTemplate(a => { a.Create("create").Read("read").Write("write"); }).Add();                
                }) 
)
[Controller]
public ActionResult Index()
        {
            List<OrderData> order = new List<OrderData>();
            List<string> list = new List<string>();
            list.Add("Cream");
            list.Add("Milk");
            list.Add("Eggs");
            list.Add("Butter");
            list.Add("Curd");
            list.Add("yogurt");
            List<string> list1 = new List<string>();
            list1.Add("Cream");
            list1.Add("Eggs");
            list1.Add("Curd");
            List<string> list2 = new List<string>();
            list2.Add("Cream");
            list2.Add("Milk");
            list2.Add("Butter");
            list2.Add("yogurt");
            var ordID = 100;
            var cusID = 200;
            for (int i = 1; i < 9; i++)
            {
                order.Add(new OrderData(cusID + 1, "Sam", ordID + 2, list));
                order.Add(new OrderData(cusID + 2, "Mercy", ordID + 4, list1));
                order.Add(new OrderData(cusID + 3, "Jim", ordID + 1, list));
                order.Add(new OrderData(cusID + 4, "Dev", ordID + 5, list2));
                order.Add(new OrderData(cusID + 5, "Jessi", ordID + 7, list1));
                order.Add(new OrderData(cusID + 6, "Cath", ordID + 3, list2));
                ordID += 1;
                cusID += 6;
            }
            ViewBag.dataSource = order;            
            var drop = list.ToList();
            var dropData = new List<object>();
            foreach (var li in drop)
            {
                dropData.Add(new { value = li, text = li });
            }
            ViewData["data"] = dropData; 
            return View();
        }
    }

 

  1. In the create event of the editTemplate, the input element for the column is created. In the write event of the editTemplate, the input text box created is converted to the ejDropDownList control with multi select option enabled.

The DataSource to the ejDropDownList control is obtained from the server side in the text value format and assigned to the control. In the read event of the edittemplate, the selected values are obtained and saved accordingly in the Grid.

JAVASCRIPT

<script type="text/javascript">
    var dropData = @Html.Raw(Json.Encode(ViewData["data"]))
    function create(args) {
        return "<input>";
    }
    function read(args) {
        return args.ejDropDownList("getValue").split(",");
    }
    function write(args) {
        var selIndex = [];
        if (args.rowdata != undefined)
        {
            for (i = 0; i < args.rowdata["OrderList"].length; i++)
            for (j = 0; j < dropData.length; j++){
                if (args.rowdata["OrderList"][i] == dropData[j].value) {
                    selIndex.push(j);
                    break;
                }
            }
        }
        args.element.ejDropDownList({ width: "100%", dataSource: dropData, fields: { id: "text", text: "text", value: "value" }, showCheckbox: true, allowMultiSelection: true, selectedItems: args.rowdata !== undefined ? selIndex : "" });
    }
</script>

 

The following screenshots displays the multi select dropdown as edit type for a column:

Initial Rendering

Figure 1: Initial Rendering

On editing a record

Figure 2: On editing a record

On saving the edited record

Figure 3: On saving the edited record



Conclusion

I hope you enjoyed learning about how to set multiselect dropdown in ASP.NET MVC Grid.

You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!



Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied