如何使控制器返回并由Razor生成的视图从api获取数据我想保留剃刀引擎视图并使用api原始mvc控制器返回视图,数据作为参数现在我希望数据来自API
MVC控制器
public class ProductController : Controller
{
public ActionResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
Api控制器
public class ProductsController : ApiController
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET api/Products
public IEnumerable<Product> GetProducts()
{
return db.Products;
}
}
Run Code Online (Sandbox Code Playgroud)
模型:
@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> …Run Code Online (Sandbox Code Playgroud)