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 > 1
                       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>0)
                {
                    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");
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM