I tried to perform filtering based on Position and Department but I’M getting this error probaply for the way I’m accessing. I tried a different way but the error still the same. I’m access it in wrong way? I Don’t know what’s the problem
Appreciate your help!
error CS0029: Cannot implicitly convert type ‘Microsoft.AspNetCore.Mvc.NotFoundObjectResult’ to ‘dotnet_rpg.Models.ServiceResponse<System.Collections.Generic.List<dotnet_rpg.Dtos.Employee.GetEmployeeDto>>’
Controller
[HttpGet("search")]
public async Task<ServiceResponse<List<GetEmployeeDto>>> SearchEmployee(string text)
{
var response = await _employeeService.searchEmployee(text);
if (response.Data == null)
{
return NotFound(response);
}
return Ok(response);
}
}
}
Service Layer
public async Task<ServiceResponse<List<GetEmployeeDto>>> searchEmployee(string text)
{
var response = new ServiceResponse<List<GetEmployeeDto>>();
var dbEmployee = await _context.Employees
.Include(a => a.FirstName)
.Where(p => p.Position.ToLower().Contains(text) ||
p.Department.ToLower().Contains(text))
.ToListAsync();
response.Data = dbEmployee.Select(c => _mapper.Map<GetEmployeeDto>(c)).ToList();
return response;
}
DataContext
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
public DbSet<Employee> Employees { get; set; }
public DbSet<User> Users { get; set; }
2
Answers
here is the answer
search
controller
Isn’t that a build error?
The controller method is expecting a type of ‘ServiceResponse<<List<GetEmployeeDto>>’ where as the code returns ‘NotFound’ or ‘Ok’, both of which are IActions.
Change the controller return type and return statements to as @owwoj mentioned.