ASP.NEt MVC utilizando la API web para devolver una vista Razor
Cómo hacer que la Vista devuelta por el controlador y generada por Razor obtenga los datos de la api Quiero mantener la vista del motor de afeitar y usar la api, el controlador mvc original devuelve la vista con los datos como parámetro ahora quiero los datos de la api
Controlador MVC
public class ProductController : Controller
{
public ActionResult Index()
{
return View();
}
Controlador Api
public class ProductsController : ApiController
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET api/Products
public IEnumerable<Product> GetProducts()
{
return db.Products;
}
}
Modelo:
@model IEnumerable<WebApplication2.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Category)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>