Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.BadRequestObjectResult'

cannot implicitly convert type list to list
cannot implicitly convert type unity
cannot implicitly convert type 'string' to 'int
cannot implicitly convert type to 'string
cannot implicitly convert type int? to int
cannot implicitly convert type to 't
cannot implicitly convert type 'double' to 'int
cannot implicitly convert type 'int' to 'bool

I have an asp.net core 2.1 project and I'm getting the following error in my controller action:

Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.BadRequestObjectResult' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)

This is my code:

[HttpPost("create")]
[ProducesResponseType(201, Type = typeof(Todo))]
[ProducesResponseType(400)]
public async Task<IList<Todo>> Create([FromBody]TodoCreateViewModel model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);   // This is the line that causes the intellisense error
    }

    await _todoRepository.AddTodo(model);
    return await GetActiveTodosForUser();
}


[HttpGet("GetActiveTodosForUser")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IList<Todo>> GetActiveTodosForUser(string UserId = "")
{
    if (string.IsNullOrEmpty(UserId))
    {
        UserId = HttpContext.User.FindFirstValue(ClaimTypes.Sid);
    }
    return await _todoRepository.GetAll(UserId, false);
}

What am I doing wrong?

Your action return type does not take in mind possible BadRequest.

Instead of direct usage of IList<Todo> you need to wrap it with generic ActionResult type.

public async Task<ActionResult<IList<Todo>>> Create(...

Here are the related docs.

[Solved] Cannot implicitly convert type 'int[]' to 'object[]'?, There is a very nice explanation for the issue here that you should read - Can't convert value type array to params object[][^]. while Inherit style in WPF it affect parent style. c#,xaml,styles,wpf-controls. If you declare a Style without an x:Key, it will override the default style for that control. <Style TargetType="local:CustomControl"> So the code above will effect all CustomControl elements throughout the entire application (or within the scope).

Actually you need to return IActionResult instead of IList for below ASP.NET Core 2.1,

public async Task<IActionResult> Create([FromBody]TodoCreateViewModel model)

Then it will work.

And for ASP.NET Core 2.1 as suggested by @amankkg,

public async Task<ActionResult<IList<Todo>>> Create([FromBody]TodoCreateViewModel model)

Casting and type conversions, C# cannot implicitly convert type when types are the same. I have a generic class​: public abstract class ModelSet<T> : ObservableCollection<T>, IModelObject  Since User inherits from Person, you cannot implicitly convert any random Person to a User (though you can implicitly convert a User to a Person). Since you pass a User[] to FindPerson(users, id), you can be sure the returned person is indeed a User, so you can cast it like this: return (User)person;

C# cannot implicitly convert type when types are the same, Cannot implicitly convert type 'Abc<Derived>' to 'IAbc<Base>' – Contravariance vs Covariance – part 2. Today I will write about the way variance  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more C# - Cannot implicitly convert type List<Product> to List<IProduct>

Cannot implicitly convert type 'Abc<Derived>' to 'IAbc<Base , The following code: IEnumerable list = new List (); Maybe > maybeList = list; // line in error gives me the error Cannot implicitly convert type  CS0029: Cannot implicitly convert type 'int' to 'bool' Hot Network Questions Can I find the acceleration or velocity when my displacement-time graph is discontinuous?

Error : Cannot implicitly convert type · Issue #65 · vkhorikov , When I want to add an id to an activity, I get this error: CS0029 C# Cannot implicitly convert type 'System.Uri' to 'string' Seems to be some  Compiler Error CS0266. 07/20/2015; 2 minutes to read +2; In this article. Cannot implicitly convert type 'type1' to 'type2'. An explicit conversion exists (are you

Activity ID: CS0029 C# Cannot implicitly convert type 'System.Uri' to , Hi I am getting above error on Controller - Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<WebApi. Casting and type conversions (C# Programming Guide) 07/20/2015; 5 minutes to read +8; In this article. Because C# is statically-typed at compile time, after a variable is declared, it cannot be declared again or assigned a value of another type unless that type is implicitly convertible to the variable's type.

Comments
  • Your function is written to return Task<IList<Todo>> and BadRequest(ModelState) obviously inst't of that type
  • That's what I was missing, thank you
  • There is a generic ActionResult in ASP.NET Core 2.1
  • I want to create the ToDo then return the list of all the ToDos for the user which is why I'm returning a List
  • can't use interface's w/ ActionResult. More here
  • @spottedmahn I'm using Interfaces with ActionResult, then how you can say that it doesn't work. ActionResult<IEnumerable<PartViewModel>> Get(), Working example is here: devniggaspk.azurewebsites.net/admin/api/course/1/Clips/…
  • Weird, it didn't work for me and the documentation says it doesn't work either.