C
C#3y ago
ckam03

Post request isn't receiving id from object

I'm not sure if this is a front end issue or backend issue but unless I pass the id from the query string, I'm not receiving it through the object.
[HttpPost]
public async Task<ActionResult> CreateEmployee([FromBody]CreateEmployeeDto employeeDto)
{
var department = await _context.Departments.FindAsync(employeeDto.DepartmentId);

if (department is null) {
return NotFound();
}

var employee = new Employee()
{
EmployeeId = Guid.NewGuid(),
FirstName = employeeDto.FirstName,
LastName = employeeDto.LastName,
PhoneNumber = employeeDto.PhoneNumber,
Salary = employeeDto.Salary,
Email = employeeDto.Email,
Department = department
};

await _context.Employees.AddAsync(employee);
await _context.SaveChangesAsync();

return Ok(employee);
}
[HttpPost]
public async Task<ActionResult> CreateEmployee([FromBody]CreateEmployeeDto employeeDto)
{
var department = await _context.Departments.FindAsync(employeeDto.DepartmentId);

if (department is null) {
return NotFound();
}

var employee = new Employee()
{
EmployeeId = Guid.NewGuid(),
FirstName = employeeDto.FirstName,
LastName = employeeDto.LastName,
PhoneNumber = employeeDto.PhoneNumber,
Salary = employeeDto.Salary,
Email = employeeDto.Email,
Department = department
};

await _context.Employees.AddAsync(employee);
await _context.SaveChangesAsync();

return Ok(employee);
}
So when I set a break point, employeeDto.DepartmentId is 0. Everything else is there. But if I do [FromQuery] int id and pass the id through the query string from the front end, I'll receive it. I'm not sure what the issue is?
2 Replies
undisputed world champions
how are you sending it? might have accidentally named it id instead of departmentId?
ckam03
ckam03OP3y ago
Yeah the name was off but would it still pass by query? strangely enough, even though it works, I'm getting a 500 error on the front end

Did you find this page helpful?