Hi pandeygolu420...,
Please refer below sample.
Model
public class DetailsModel
{
public int Id { get; set; }
public string Value{ get; set; }
}
Controller
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult AjaxMethod(string name)
{
AjaxSamplesEntities2 entity = new AjaxSamplesEntities2();
List<DetailsModel> customers = (from customer in entity.Customers.Take(10)
select new DetailsModel
{
Id = customer.CustomerId,
Value = customer.Name
}).ToList();
return Json(customers);
}
}
View
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
padding-left: 20px;
}
</style>
</head>
<body>
<select id="ddlCustomers" multiple="multiple">
</select>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js">
</script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "/Home/AjaxMethod",
data: '{}',
success: function (response) {
var ddlCustomers = $("#ddlCustomers");
ddlCustomers.empty();
$.each(response, function () {
ddlCustomers.append($("<option></option>").val(this['Id']).html(this['Value']));
});
$("#ddlCustomers").select2();
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
});
</script>
</body>
</html>
Screenshot