Reputation: 441
i'm trying to send data to save it in my db, basic, so i send a model to my controller, and i get this error:
"Each parameter in constructor 'Void .ctor(SmgApi.Models.Entity.EquipmentEntity)' on type 'SmgApi.Models.Dto.EquipementDto' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive."
but i don't deserialize anything, i don't understand. my controller :
[HttpPost]
public async Task<IActionResult> UpdateEquipment(EquipementDto equipment)
{
return Ok(await _equipmentRepository.UpdateEquipment(new EquipmentEntity(equipment)));
}
EquipmentDto:
public class EquipementDto
{
[Required]
public string Id { get; set; }
public List<PropertyDto> Properties { get; set; }
[Required]
public string Type { get; set; }
public bool isInPalette { get; set; }
public EquipementDto(EquipmentEntity equipment)
{
if (equipment == null)
return;
this.Id = equipment.Id;
this.Type = equipment.Type;
this.isInPalette = equipment.IsInPalette;
this.Properties = equipment.Properties.Select(x => new PropertyDto(x)).ToList();
}
}
my Equipment interface in front:
export interface Equipment {
id: string;
type: Type;
isInPalette: boolean;
properties: Array<Property>;
}
my request:
private equipmentUrl: string = environment.apiUrl + '/equipment';
constructor(private http: HttpClient) {}
public saveEquipment(equipment: Equipment): Observable<Equipment> {
return this.http.post<Equipment>(this.equipmentUrl, equipment);
}
Any ideas on what I'm doing wrong?
Upvotes: 41
Views: 60183
Reputation: 171
my problem was i needed to a add an empty constructor
public class xpto
{
public xpto(){}
}
Upvotes: 6
Reputation:
but i don't deserialize anything, i don't understand.
You might not do it directly, but you certainly ask the framework to deserialize something for you:
[HttpPost]
public async Task<IActionResult> UpdateEquipment(EquipementDto equipment)
Here you tell the framework you expect the call to contain a serialized string of json representing an object matching your EquipementDto
and to please deserialize that string for you into an actual instance of the EquipementDto
and pass that to the method.
So now that we've determined where the deserialization takes place we can take a look at what's going on. During deserialization the framework that does the deserialization needs to be able to construct the object you want deserialized.
To be able to construct that object it needs a valid constructor. It seems like it'll accept 2 types of constructors:
Your EquipementDto
has only 1 constructor and that constructor takes some unknown EquipmentEntity
that the deserialization framework knows nothing about.
Now I'm not completely sure but I think this will be solved by just adding a parameterless constructor to your EquipementDto
class:
[JsonConstructor]
public EquipementDto(){}
Upvotes: 64