本实例使用Linq to sql.
DB所需要的表:1.Product,2.Category 如下图
1.先创建列表页面
先创建Index 的Control,在Control文件夹下创建ProductControl 文件(在这之前你要使用Linq to sql 创建TestDB文件与DB通信,我这里使用的数据库是Test,并将Product,category 添加到TestDB.dbml文件中)
public class ProductController : Controller
{
//
// GET: /Product/
//TESTEntities dbConnect = new TESTEntities();
TestDBDataContext context = new TestDBDataContext();
ProductModelView productView = new ProductModelView();
public ActionResult Index(string searchString)
{
var products = from product in context.Products
select product;
if (!string.IsNullOrEmpty(searchString))
{
products = products.Where(p => p.ProductName == searchString);
}
return View(products.ToList());
}
}
在Model 文件夹中创建ProductModelView文件
public class ProductModelView
{
public Product productList { get; set; }
}
在View文件夹中创建Product文件夹,然后在创建Index.cshtml
@model List<MusicStore.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.ActionLink("New Create", "Create")
@using (Html.Begin