2
This URL works on Postman: http://localhost:56137/api/Getcidade. I need now in another MVC project to bring the list of cities. When I do this I catch this mistake:
The template item inserted in the dictionary is from type'System.Threading.Tasks.Task
1[System.Collections.Generic.List
1[Trainingcrud.Models.City]', but this dictionary requires an item like 'System.Collections.Generic.Ienumerable`1[Trainingcrud.Services.Getcitiesasync]'.
In the debug I arrive at the controller
public ActionResult Index()
{
GetCidadesAsync cidadeAsync = new GetCidadesAsync();
var model = cidadeAsync.GetCidades();
return View(model);
}
And also from that controller, I arrive in Getcidades()
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 you get on that line: var response = await client.GetStringAsync(url);
, he opens the method in my project which is in another project, another Solution. This is the method which 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 list of cities from the bank. The method in my API to take the list from the bank
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 making this mistake?
EDIT1
I changed the cshtml model and now this error has come
The template item inserted in the dictionary is from type'System.Threading.Tasks.Task
1[System.Collections.Generic.List
1[Trainingcrud.Models.City]', but this dictionary requires an item like 'System.Collections.Generic.Ienumerable`1[Trainingcrud.Models.City]'.
Pretty much the same thing, except they’re pointing to the same place Trainingcrud.Models.City