我是 Kendo UI 和 ASP.Net MVC4 的新手。我正在开发一个购物车项目。我想使用数据库表中的数据填充 ListView。在数据库内部,我有一个类别表,我想在 ListView 中显示所有类别名称和 ID。在此先感谢,请帮助我。
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopCart300714.Models;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
namespace ShopCart300714.Controllers
{
public class ShopCartController : Controller
{
//
// GET: /ShopCart/
Shopping_CartEntities sc = new Shopping_CartEntities();
public ActionResult Index()
{
//var cat = sc.CATEGORies.ToList();
ViewBag.Products = sc.CATEGORies;
return View(GetItems());
}
public ActionResult Product(int catID)
{
var prod = from p in sc.PRODUCTs
where p.CAT_ID == catID
select new
{
p.PROD_ID,
p.PROD_NAME,
p.COST
};
return View(prod);
}
public ActionResult Products_Read([DataSourceRequest] DataSourceRequest request)
{
return Json(GetItems().ToDataSourceResult(request));
}
private static IEnumerable<CATEGORY> GetItems()
{
var db = new Shopping_CartEntities();
//var categories=from c in db.CATEGORies
// select c
return db.CATEGORies.Select (product => new CATEGORY
{
CAT_ID = product.CAT_ID,
CAT_NAME = product.CAT_NAME,
});
}
}
}
看法:
@using Kendo.Mvc.UI;
@model IEnumerable<ShopCart300714.Models.CATEGORY>
@*@{
ViewBag.Title = "Index";
}*@
<h2>Categories</h2>
<script type="text/x-kendo-tmpl" id="template">
<div class="product">
<img src="@Url.Content("~/content/web/foods/")#:ProductID#.jpg" alt="#:PROD_NAME# image"/>
<h3>#:PROD_NAME#</h3>
</div>
</script>
<div class="demo-section">
@(Html.Kendo().ListView<ShopCart300714.Models.CATEGORY>(Model)
.Name("listView")
.TagName("div")
.ClientTemplateId("template")
.DataSource(dataSource =>
{
dataSource.Read(read => read.Action("Products_Read", "ShopCart"));
dataSource.PageSize(15);
})
.Pageable()
)