estoy ocupando una API de ligas de futbol con .NET, la transformo de json a un objeto y pasando con ViewBag
a la vista. Cuando lo quiero imprimir me llega como nulo.
Aqui esta la clase:
namespace consumir_api.Controllers
{
class Countries
{
public string countryId{ get; set; }
public string countriyName { get; set; }
}
}
el controlador:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
namespace consumir_api.Controllers
{
public class HomeController : Controller
{
public async Task<ActionResult> Index()
{
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync("https://apifootball.com/api/?action=get_countries&APIkey=0fa4f05e7f7e95c4d9efbb3cf2a405f85c53350a8dcf40ba1311dfcde751da4f");
ViewBag.result = JsonConvert.DeserializeObject<List<Countries>>( json );
return View();
}
}
}
vista:
<div class="jumbotron">
<table>
<thead>
<tr>
<th> Id</th>
<th> Nombre</th>
</tr>
</thead>
<tbody>
@foreach ( var countries in ViewBag.result )
{
<tr>
<td>
@countries.country_id
</td>
<td>
@countries.country_name
</td>
</tr>
}
</tbody>
</table>
</div>