如何在mvc4中将列表绑定到gridview

时间:2013-11-11 04:45:16

标签: asp.net-mvc-4 asp.net-web-api

我正在创建一个带有web api的演示网络互联网mvc4,现在我得到了我无法将列表绑定到gridview或表格以显示项目的存根,这是我的代码。

我的控制器,

这里是json web api return:

[{"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展示产品清单吗?非常感谢

0 个答案:

没有答案