I can not get list of an API

2

This URL works on postman: link . I need now in another MVC project to bring the list of cities. When I do this I get this error:

  

The template item entered in the dictionary is from   type 'System.Threading.Tasks.Task 1[System.Collections.Generic.List 1 [TrainingChud.Models.City]]',   but this dictionary requires an item of type   'System.Collections.Generic.IEnumerable'1 [TrainingCrud.Services.GetCitiesAsync]'.

In the debug I get into the controller

public ActionResult Index()
{
    GetCidadesAsync cidadeAsync = new GetCidadesAsync();
    var model = cidadeAsync.GetCidades();
    return View(model);
}

And also from that controller, I get to GetCities ()

public class GetCidadesAsync
    {
        HttpClient client = new HttpClient(); 
        public async Task<List<Cidade>> GetCidades()
        {
            string url = $"http://localhost:56137/api/GetCidade";
            var response = await client.GetStringAsync(url);
            var _cidade = JsonConvert.DeserializeObject<List<Cidade>>(response);

            return _cidade.ToList();
        }
    }

When it arrives at this line: var response = await client.GetStringAsync(url); , it opens the method in my project that is in another project, another solution. This is the method that is in another solution

[RoutePrefix("api/[controller]")]
    public class GetCidadeController : ApiController
    {
        GetCidade getCidades = new GetCidade();

        [HttpGet]
        public IEnumerable<Cidade> GetCidadesAsync()
        {
            return getCidades.GetCidades().AsEnumerable().ToList();
        }

    }

This is the controller that takes the relation of cities from the bank. The method in my API to get the database from the list

public class GetCidade
    {
        BancoContext banco = new BancoContext();  
        public List<Cidade> GetCidades()
        {
            return banco.Database.SqlQuery<Cidade>("sp_cons_cidade").ToList();
        }
    }

My Model

public class Cidade
{
  [Key]
  public int id { get; set; }
  [Required]         
  public String nome { get; set; }
}

Why am I having this error?

EDIT1

I changed the cshtml model and now came this error

  

The template item entered in the dictionary is from   type 'System.Threading.Tasks.Task 1[System.Collections.Generic.List 1 [TrainingChud.Models.City]]',   but this dictionary requires an item of type   'System.Collections.Generic.IEnumerable'1 [TrainingCrud.Models.City]'.

Pretty much the same thing, except they're pointing to the same place TrainingCrud.Models.City

    
asked by anonymous 09.08.2018 / 18:04

1 answer

2

At your control of Index , change the method return to Task<ActionResult> and use await to wait for the return of the GetCidades method, as this method returns a Task and not a Cidade collection. % (as the error message says):

public async Task<ActionResult> Index()
{
    GetCidadesAsync cidadeAsync = new GetCidadesAsync();
    var model = await cidadeAsync.GetCidades();
    return View(model);
}

It is important to always be aware when returning Task , because somehow, you must return this task and wait for the end of it to gain access to the result object. And that's what await does

    
09.08.2018 / 18:42