视图 Model转集合
@{
Layout = null;
}
@using MvcApplication2.Models
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
@foreach (var item in Model as List<Books>)
{
<tr>
<td>@item.Title</td>
<td><a href="/Book/Delete/@item.Id">删除</a></td>
</tr>
}
</table>
</div>
</body>
</html>
public ActionResult Index()
{
List<Books> list=db.Books.Take(10).ToList();
return View(list);
}
public ActionResult Delete(int id)
{
Books mod = (from b in db.Books where b.Id == id select b).FirstOrDefault();
//Books mod=db.Books.Where(b => b.Id == id).FirstOrDefault();
if (mod != null)
{
db.Books.Remove(mod);
db.SaveChanges();
return Redirect("/Book/Index");
}
return Content("not found");
}