题目:
实现过程
控制器代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index(string name)
{
if (name==null)
{
ViewBag.list = BLL.PlayerManager.Show();
return View();
}
else
{
ViewBag.list = BLL.PlayerManager.Find(name);
return View();
}
}
public ActionResult Delect(int id) {
BLL.PlayerManager.Delect(id);
return RedirectToAction("Index");
}
public ActionResult Deile(int id)
{
var model= DAL.PlayerServices.FindModel(id);
ViewBag.goodname = model.goodname;
ViewBag.price = model.price;
ViewBag.launchtime = model.launchtime;
ViewBag.description = model.description;
return View();
}
}
}
DAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication1.Models;
namespace MvcApplication1.DAL
{
public class PlayerServices
{
public static List<good> Show() {
ProductDBEntities db = new ProductDBEntities();
return db.goods.ToList();
}
public static List<good> Find(string name)
{
ProductDBEntities db = new ProductDBEntities();
return db.goods.Where(x => x.goodname.Contains(name)).ToList();
}
public static good FindModel(int id)
{
ProductDBEntities db = new ProductDBEntities();
return db.goods.SingleOrDefault(x => x.goodid == id);
}
public static bool Delect(int id)
{
ProductDBEntities db = new ProductDBEntities();
db.Entry(FindModel(id)).State = System.Data.EntityState.Deleted;
return db.SaveChanges() > 0;
}
}
}
BLL
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MvcApplication1.Models; namespace MvcApplication1.BLL { public class PlayerManager { public static List<good> Show() { return DAL.PlayerServices.Show(); } public static List<good> Find(string name) { return DAL.PlayerServices.Find(name); } public static bool Delect(int id) { return DAL.PlayerServices.Delect(id); } } }
Index
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<form action="/Home/Index">
<label>关键词</label><input type="text" name="name"/><input type="submit" value="查询">
</form>
<table border="1">
<tr>
<th>商品名称</th>
<th>价格</th>
<th>上架时间</th>
<th>操作</th>
</tr>
@foreach (var item in @ViewBag.list as List<MvcApplication1.Models.good>)
{
<tr>
<td>@item.goodname</td>
<td>@item.price</td>
<td>@item.launchtime.ToString("yyyy年MM月dd日")</td>
<td>@Html.ActionLink("删除", "Delect", new { id = @item.goodid }, new { onclick="return confirm('确定删除吗?')"})
@Html.ActionLink("详情", "Deile", new { id = @item.goodid })
</td>
</tr>
}
</table>
</div>
</body>
</html>
Deile
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Deile</title>
</head>
<body>
<div>
<label>商品名称:</label>@ViewBag.goodname<br />
<label>商品价格:</label>@ViewBag.price<br />
<label>上架时间:</label>@ViewBag.launchtime<br />
<label>商品详情:</label>@ViewBag.description<br />
<a href="/Home/Index">返回首页</a>
</div>
</body>
</html>