ckam03
ckam03
CC#
Created by ckam03 on 8/19/2022 in #help
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?
4 replies