小编use*_*484的帖子

ASP.NEt MVC使用Web API返回Razor视图

如何使控制器返回并由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)

html c# asp.net-mvc razor asp.net-web-api

17
推荐指数
2
解决办法
5万
查看次数

标签 统计

asp.net-mvc ×1

asp.net-web-api ×1

c# ×1

html ×1

razor ×1