I am new in ASP.NET Core. I just want to simply load a JSON file's data into a listbox.
Here is the controller class:
public class EmployeeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpGet]
public JsonResult GetEmpList()
{
var empList = new List<Employee>()
{
new Employee { Id=1, Name="Manas"},
new Employee { Id=2, Name="Tester"}
};
return Json(empList);
}
}
And here is the view:
<select id="ddlEmployees"></select>
<script>
var ddlEmployees = $("#ddlEmployees");
$.ajax({
type: "GET",
url: "/EmployeeController/GetEmpList",
dataType: 'JSON',
contentType: "application/json",
success: function (data) {
alert(data);
for (var i = 0; i < data.length; i++) {
ddlEmployees.append('<option> ' + data[i] + '</option >');
}
}
}); </script>