I have the following code and it return undefined value using ajax.
public List<Employee> ListAll()
{
List<Employee> lst = new List<Employee>();
connection();
SqlCommand com = new SqlCommand("SELECT EmployeeID, LastName, FirstName, City FROM Employees ORDER BY EmployeeID DESC", con);
SqlDataReader rdr = com.ExecuteReader();
while (rdr.Read())
{
lst.Add(new Employee
{
EmployeeID = Convert.ToInt32(rdr["EmployeeId"]),
LastName = rdr["LastName"].ToString(),
FirstName = rdr["FirstName"].ToString(),
City = rdr["City"].ToString(),
});
}
return lst;
}
public JsonResult List()
{
return Json(ListAll().ToList());
}
<!-- jQuery (necessary for Bootstrap's JavaScript plugins and AJAX) -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- Bootstrap JS -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
<script>
// Load Data in Table when document is ready
$(document).ready(function () {
loadData();
});
// Load Data function
function loadData() {
$.ajax({
url: "/NE/List",
type: "GET",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
var html = '';
$.each(result, function (key, item) {
html += '<tr>';
html += '<td>' + item.EmployeeID + '</td>';
html += '<td>' + item.FirstName + '</td>';
html += '<td>' + item.LastName + '</td>';
html += '<td>' + item.City + '</td>';
html += '<td><a href="#" onclick="return getbyID(' + item.EmployeeID + ')">Edit</a> | <a href="#" onclick="Delele(' + item.EmployeeID + ')">Delete</a></td>';
html += '</tr>';
});
$('.tbody').html(html);
},
error: function (errormessage) {
(errormessage.responseText);
}
});
}
</script>
<p class="container">
<h2>Employees Record</h2>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" onclick="clearTextBox();">
Add New Employee
</button><br /><br />
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Last Name</th>
<th>City</th>
<th>Action</th>
</tr>
</thead>
<tbody class="tbody">
<!-- Employee records will be populated here -->
</tbody>
</table>
</p>