asp.net mvc6+ef框架做的书籍管理项目

时间:2023-03-08 17:07:14
asp.net mvc6+ef框架做的书籍管理项目

效果图:

asp.net mvc6+ef框架做的书籍管理项目

目录结构:asp.net mvc6+ef框架做的书籍管理项目

book控制器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace WebApplication2.Controllers
{
public class BookController : Controller
{
//引入ef
Models.qrabEntities entity = new Models.qrabEntities(); // GET: 书籍列表
public ActionResult BookList()
{
var book = from b in entity.Book
where b.id >
select b;
return View(book.ToList());
}
//添加一本书籍
public ActionResult Create()
{
var booktype = from s in entity.BookType
select s;
List<SelectListItem> items = new List<SelectListItem>();
foreach(var item in booktype)
{
SelectListItem selectItem = new SelectListItem()
{
Text = item.typename,
Value = item.id.ToString()
};
items.Add(selectItem);
}
ViewData["typeid"] = items;
return View();
}
//添加书籍post提交
[HttpPost]
public ActionResult Create(Models.Book book)
{
if (ModelState.IsValid)
{
string strTypeid = Request.Form["typeid"];
int intid = Convert.ToInt32(strTypeid);
var typeList = from s in entity.BookType
where s.id == intid
select s;
book.BookType = typeList.FirstOrDefault();
entity.Book.Add(book);
entity.SaveChanges();
return RedirectToAction("BookList");
}
else
{
var booktype = from s in entity.BookType
select s;
List<SelectListItem> items = new List<SelectListItem>();
foreach (var item in booktype)
{
SelectListItem selectItem = new SelectListItem()
{
Text = item.typename,
Value = item.id.ToString()
};
items.Add(selectItem);
}
ViewData["typeid"] = items;
return View(book);
}
}
//书籍详情
public ActionResult Details(int id)
{
var book = from s in entity.Book
where s.id == id
select s;
Models.Book bookModel = book.FirstOrDefault();
if(bookModel!=null)
{
return View("Details", bookModel);
}
else
{
return RedirectToAction("BookList");
}
}
//编辑书籍
public ActionResult Edit(int id)
{
var book = from s in entity.Book
where s.id == id
select s;
Models.Book bookModel = book.FirstOrDefault();
if(bookModel!=null)
{
//外键关系 找出当前书本所对于的分类信息
var booktype = from s in entity.BookType
where s.id == bookModel.typeid
select s;
//取出所以的书籍分类,绑定到dropdownlist中
var booktypes = from s in entity.BookType
select s;
List<SelectListItem> items = new List<SelectListItem>();
foreach(var item in booktypes)
{
SelectListItem selectItem = null;
if(item.id==bookModel.typeid)
{
selectItem = new SelectListItem()
{
Text = item.typename,
Value = item.id.ToString(),
Selected = true
};
}
else
{
selectItem = new SelectListItem()
{
Text = item.typename,
Value = item.id.ToString()
};
}
items.Add(selectItem);
}
ViewData["typeid"] = items;
return View("Edit", bookModel);
}
else
{
return RedirectToAction("BookList");
}
}
//编辑书籍post提交书籍
[HttpPost]
public ActionResult Edit(Models.Book book)
{
try
{
var bookold = entity.Book.Find(book.id);//取出修改前的数据模型
string strbooktypeid = Request.Form["typeid"];
bookold.name = book.name;
bookold.txt = book.txt;
bookold.typeid = Convert.ToInt32(strbooktypeid);
entity.Entry<Models.Book>(bookold).State = System.Data.Entity.EntityState.Modified;
int intCount = entity.SaveChanges();
if(intCount>)
{
return RedirectToAction("Details", new { id = book.id });
}else
{
return Content("修改失败");
}
}
catch (Exception)
{
return Content("修改失败");
}
}
//删除书籍
public ActionResult Delete(int id)
{
//var book = entity.Book.Find(id);
var book1 = from s in entity.Book
where s.id == id
select s;
Models.Book book = book1.FirstOrDefault();
return View("Delete", book);
}
//post提交确认删除书籍
[HttpPost]
public ActionResult Delete(int id,FormCollection collection)
{
//var book = entity.Book.Find(id);
var book1 = from s in entity.Book
where s.id == id
select s;
Models.Book book = book1.FirstOrDefault();
//多本书可以循环删除
//foreach(var item in book)
//{
// entity.Book.Remove(item);
//}
entity.Book.Remove(book);
entity.SaveChanges();
return RedirectToAction("BookList");
}
}
}