0

我正在使用 web api 创建一个演示 web internet mvc4,现在我得到了无法将列表绑定到 gridview 或 table 以显示项目的存根,这是我的代码。

我的控制器,

这是 json web api 返回:

[{"Id":1,"Name":"test2","Category":"test","price":23.0},
{"Id":2,"Name":"test1","Category":"test","price":12.0}]

这是控制器,

public ActionResult Index()
 {
  HttpClient client = new HttpClient();
  client.BaseAddress = new Uri("http://localhost:54854");

  // Add an Accept header for JSON format.
  client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
  HttpResponseMessage response = client.GetAsync("api/Values").Result;
  if (response.IsSuccessStatusCode)
  {
   var products = response.Content.ReadAsAsync<IEnumerable<Products>>().Result;
    return View(products);
  }
  else
  {
   return View();
  }

模态

public class Products
    {
        public int Id { get; set; }
        public String Name { get; set; }
        public String Category { get; set; }
        public Double price { get; set; }
    }

查看 Index.cshtml

@{
    ViewBag.Title = "Index";
}

请您继续向 gridview 显示产品列表吗?非常感谢您

4

0 回答 0