Multi-Select Dropdown List in ASP.NET

asp Dropdownlist multiple item selection

The DropDownList

allows the user to select a single item from a drop-down list

A ListBox allows you to select multiple items:

<asp:ListBox CssClass="selectpicker" 
ID="DropDownList1"
runat="server"
data-live-search="true"
SelectionMode="Multiple">
<asp:ListItem>Brasil</asp:ListItem>
<asp:ListItem>Colombia</asp:ListItem>
<asp:ListItem>United States</asp:ListItem>
<asp:ListItem>Frannce</asp:ListItem>
<asp:ListItem>Italy</asp:ListItem>
<asp:ListItem>Japan</asp:ListItem>
</asp:ListBox>

The rest of your code should work as is, maybe update the ID. Aspsnippets have an example using bootstrap.

Multi-select dropdown list in ASP.NET

You could use the System.Web.UI.WebControls.CheckBoxList control or use the System.Web.UI.WebControls.ListBox control with the SelectionMode property set to Multiple.

How to use multi select dropdown list asp net

You need change id from example-getting-started to ListBox1

<script type="text/javascript">
$(document).ready(function() {
$('#ListBox1').multiselect();
});
</script>

How to access multiselect dropdownlist using asp.net c#

I hope in this case avoid DropDownList and use ListBox.

Take a look at the ListBox control to allow multi-select.

<asp:ListBox runat="server" ID="lblMultiSelect" SelectionMode="multiple">
<asp:ListItem Text="One" Value="OneM" />
<asp:ListItem Text="Two" Value="TwoM" />
<asp:ListItem Text="Three" Value="ThreeM" />
</asp:ListBox>

in the code behind

foreach(ListItem listItem in lblMultiSelect.Items)
{
if (listItem.Selected == True)
{
var val = listItem.Value;
var txt = listItem.Text;
}
}

Else go for Dropdown with checkboxes

jQuery Dropdown Check List

System.Web.UI.WebControls.CheckBoxList

ASP.NET MVC multiple select dropdown

In your view:

@Html.ListBoxFor(m => m.location_code, Model.location_type)

That's all you need. You're using a ListBox control so it's already a multiple select list.

Then back in your controller you can get the selected items like this:

[HttpPost]
public string SaveResults(List<int> location_code)
{

if (location_code!= null)
{
return string.Join(",", location_code);
}
else
{
return "No values are selected";
}
}

How to Create MultiSelect dropdown with checkboxes in Asp.NET MVC

Here is a sample, base on @jishan siddique suggestion.
You can visit the link to refer more: http://davidstutz.github.io/bootstrap-multiselect/

Model

public class ProductViewModel
{
public List<SelectListItem> Products { get; set; }
public int[] ProductIds { get; set; }
}

Controller

public IActionResult Index()
{
var model = new ProductViewModel()
{
Products = GetProducts()
};
return View(model);
}

[HttpPost]
public ActionResult Index(ProductViewModel product)
{
return View(product);
}

private List<SelectListItem> GetProducts()
{
var data = new List<SelectListItem>()
{
new SelectListItem()
{
Value = "1", Text = "Tomato"
},
new SelectListItem()
{
Value = "2", Text = "Orange"
},
new SelectListItem()
{
Value = "3", Text = "Potato"
}
};
return data;
}

Views

@model CiberProject.ViewModels.ProductViewModel
@{
ViewData["Title"] = "Home Page";
}

<link href="https://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.Label("Products:")
<br />
<br />
@Html.ListBoxFor(m => m.ProductIds, Model.Products, new { @class = "listbox" })
<br />
<br />
<input type="submit" value="Submit" />
}

@section Scripts
{
<script src="https://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('.listbox').multiselect({
includeSelectAllOption: true
});
});
</script>
}


Related Topics



Leave a reply



Submit