Hello everyone, community, I have a problem that I get a list of objects, but when I want to update the same list of objects that I get, it is giving me an error, I am going through the list with foreach so that it reads each object from the list, but it does not It is recognizing, the input variable is the model of the RolPermiso class and it is a post method and I send the list of objects as json, what is my error?
this is my repository
public string updateRolPermiso(RolPermiso rp)
{
string rpta = "";
List<RolPermiso> rolPermiso = _condominioContext.RolPermiso.Where(x => x.RolPerCod.Equals(rp.RolPerCod)).ToList();
foreach (var item in rolPermiso)
{
item.RolPerCod = rp.RolPerCod;
item.RolPerCodRol = rp.RolPerCodRol;
item.RolPerCodPer = rp.RolPerCodPer;
item.RolPerEst = rp.RolPerEst;
item.RolPerFecCre = rp.RolPerFecCre;
item.RolPerUsuCre = rp.RolPerUsuCre;
item.RolPerFecMod = rp.RolPerFecMod;
item.RolPerUsuMod = rp.RolPerUsuMod;
}
_condominioContext.SaveChanges();
rpta = "Los permisos se actualizaron correctamente";
return rpta;
}
this is my controller
[HttpPost("actualizarRolPer")]
public ActionResult actualizarRolPer([FromBody] RolPermiso rolPermiso)
{
IActionResult result = Unauthorized();
string rpta = _adminService.updateRolPermiso(rolPermiso);
try
{
if (rpta.Equals("Los permisos se actualizaron correctamente"))
{
result = Ok(new { messaje = rpta, error = false, data = new { permisos = rolPermiso } });
}
else
{
result = Ok(new { messaje = "No se pudo actualizar los permisos", error = true });
}
}
catch
{
result = Ok(new { messaje = "Sé origino un error en el proceso", error = true });
}
return Ok(result);
}
the json that I send
[
{
"rolPerCod": 1,
"rolPerCodRol": 1,
"rolPerCodPer": 1,
"rolPerEst": "0",
"rolPerFecCre": "2021-05-28T09:59:03.82",
"rolPerUsuCre": "",
"rolPerFecMod": null,
"rolPerUsuMod": null
},
{
"rolPerCod": 2,
"rolPerCodRol": 1,
"rolPerCodPer": 1,
"rolPerEst": "0",
"rolPerFecCre": "2021-05-28T10:04:59.387",
"rolPerUsuCre": "",
"rolPerFecMod": null,
"rolPerUsuMod": null
}
]
in postman it throws me this error
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-d3cfa013ca4db34ca0d4ec28609d5e22-a89c280de6967a4a-00",
"errors": {
"$": [
"The JSON value could not be converted to Condominios.Models.RolPermiso. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
]
}
}
What happens is that the json you are sending is a collection and in the controller it expects an object of the class
RolPermiso
, try changing the json or in the controller expect anRolPermiso
example collection (public ActionResult actualizarRolPer([FromBody] RolPermiso[] rolPermisos)
) now if you do this you have to iterate over that collection and call the method_adminService.updateRolPermiso(rolPermiso);
for each element of the collection example:and then send a collection of messages for each permission,
What would I do, I would change the json and send a single object instead of a collection, Greetings