Hello! I'm a desktop app and videogame developer and I'm learning web development. I followed a tutorial about CRUD in ASP.NET Core MVC, which lists, adds, edits, and removes employees' data. My app starts with an html table with the employees' list, each field in a <td> (a row cell); all fields are individual data (like name, salary, etc.). But I want to add a variable list of phone numbers for each employee; for that, I added a new table Telefono to the DB, which is linked to table Empleado through the idEmpleado FK field, and I'm trying to place those numbers into options of a select object placed in the <td> of the table Empleado list. All this code I'm talking about is JavaScript.
Below I show the C# method that is fetched to load the Empleado (employee) list, which is located in the Controller class. It works fine.
Below I show the C# method that is fetched to load the list of Telefono (phone number) objects from a specific Empleado (employee). You can see that there I changed the return type, I replaced the Status200OK return type by the List<Telefono> itself, because I think that that was in conflict with the data retrieving in JavaScript code. Currently it is not working (this method and/or the JavaScript function which fetches it). Actually I don't exactly know it the changes I made are correct or not, and how it would be the correct way, because by now, all these methods just loaded one list and returned Status200OK, but now I need to load the variable Telefono list first (without returning Status200OK, that is the problem I think) and then pass them to the Empleado list (add <option>s to the <select> in the <td>). It doesn't work (the JS returns an empty array).
Below I show the JavaScript function which loads and shows the Empleado list (without Telefono objects by now). It works fine.
Below I show the JavaScript function that should load the variable list of Telefono objects for a specific Empleado, through the idEmpleado FK field. With its corresponding C# method I showed above, it is returning an empty array (I saw while debugging). So, maybe (or very probably) the syntax of this function is not correct, so as its C# method's one. It doesn't work (it returns an empty array).
So, summarizing, what I'm looking for is to get data (a phone number variable list) from the C# method in the Controller (the DB, SPs, and ADO.Net code in the Repository class are perfect) from the JavaScript function (the MostrarEmpleados() function precisely) in order to add that Telefono variable list to an <option> list in the <select> (dropdown list) which would be appended to the <td> (row cell) in the Empleado list. My two great doubts are the syntax and contents of the listaTelefonos() C# method in the Controller and the ObtenerTelefonos(id) JavaScript function which fetches (or it should) that C# method. I suppose the C# method shouldn't return Status200OK or so, and the JS method is incorrect too (stuff about response and that), but have no idea on how to write it correctly.
I know there are some very good programmers here, I hope someone helps me with this.
I will apreciate your help very much. Thank you in advance.
Pablo
Below I show the C# method that is fetched to load the Empleado (employee) list, which is located in the Controller class. It works fine.
C#:
[HttpGet]
public async Task<IActionResult> listaEmpleados()
{
List<Empleado> _lista = await _empleadoRepository.Lista();
return StatusCode(StatusCodes.Status200OK, _lista);
}
Below I show the C# method that is fetched to load the list of Telefono (phone number) objects from a specific Empleado (employee). You can see that there I changed the return type, I replaced the Status200OK return type by the List<Telefono> itself, because I think that that was in conflict with the data retrieving in JavaScript code. Currently it is not working (this method and/or the JavaScript function which fetches it). Actually I don't exactly know it the changes I made are correct or not, and how it would be the correct way, because by now, all these methods just loaded one list and returned Status200OK, but now I need to load the variable Telefono list first (without returning Status200OK, that is the problem I think) and then pass them to the Empleado list (add <option>s to the <select> in the <td>). It doesn't work (the JS returns an empty array).
C#:
[HttpGet]
public async Task<List<Telefono>> listaTelefonos(int idEmpleado)
{
List<Telefono> _lista = await _telefonoRepository.Lista(idEmpleado);
return _lista;
}
Below I show the JavaScript function which loads and shows the Empleado list (without Telefono objects by now). It works fine.
JavaScript:
function MostrarEmpleados() {
fetch("/Home/listaEmpleados")
.then(response => {
return response.ok ? response.json() : Promise.reject(response)
})
.then(responseJson => {
$("#tablaEmpleados tbody").html("");
if (responseJson.length > 0) {
responseJson.forEach((empleado) => {
$("#tablaEmpleados tbody").append(
$("<tr>").append(
$("<td>").text(empleado.nombreCompleto),
$("<td>").text(empleado.refDepartamento.nombre),
$("<td>").text(empleado.sueldo),
$("<td>").text(empleado.fechaContrato),
$("<td>").append(
$("<button>").addClass("btn btn-primary btn-sm boton-editar-empleado").text("Editar").data("dataEmpleado", empleado),
$("<button>").addClass("btn btn-danger btn-sm ms-2 boton-eliminar-empleado").text("Eliminar").data("dataEmpleado", empleado),
)
)
)
})
}
})
}
Below I show the JavaScript function that should load the variable list of Telefono objects for a specific Empleado, through the idEmpleado FK field. With its corresponding C# method I showed above, it is returning an empty array (I saw while debugging). So, maybe (or very probably) the syntax of this function is not correct, so as its C# method's one. It doesn't work (it returns an empty array).
JavaScript:
function ObtenerTelefonos(id) {
return fetch(`/Telefono/listaTelefonos?idEmpleado=${id}`)
.then(response => {
return response.ok ? response.json() : Promise.reject(response)
})
.then(responseJson => {
const telefonos = [];
if (responseJson.length > 0) {
responseJson.forEach((telefono) => {
telefonos.push({
idTelefono: telefono.idTelefono,
descripcion: telefono.descripcion,
numero: telefono.numero,
idEmpleado: telefono.idEmpleado
});
})
}
return telefonos;
})
}
So, summarizing, what I'm looking for is to get data (a phone number variable list) from the C# method in the Controller (the DB, SPs, and ADO.Net code in the Repository class are perfect) from the JavaScript function (the MostrarEmpleados() function precisely) in order to add that Telefono variable list to an <option> list in the <select> (dropdown list) which would be appended to the <td> (row cell) in the Empleado list. My two great doubts are the syntax and contents of the listaTelefonos() C# method in the Controller and the ObtenerTelefonos(id) JavaScript function which fetches (or it should) that C# method. I suppose the C# method shouldn't return Status200OK or so, and the JS method is incorrect too (stuff about response and that), but have no idea on how to write it correctly.
I know there are some very good programmers here, I hope someone helps me with this.
I will apreciate your help very much. Thank you in advance.
Pablo