效果图:

目录结构:

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

asp.net mvc6+ef框架做的书籍管理项目的更多相关文章

  1. ASP.NET MVC+EF框架+EasyUI实现权限管理系列

    http://www.cnblogs.com/hanyinglong/archive/2013/03/22/2976478.html ASP.NET MVC+EF框架+EasyUI实现权限管理系列之开 ...

  2. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(21)-用户角色权限基本的实现说明

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(21)-用户角色权限基本的实现说明     ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框 ...

  3. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(20)-多条件模糊查询和回收站还原的实现

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(20)-多条件模糊查询和回收站还原的实现 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架 ...

  4. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(19)-用户信息的修改和浏览

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(19)-用户信息的修改和浏览  ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    ...

  5. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(18)-过滤器的使用和批量删除数据(伪删除和直接删除)

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(18)-过滤器的使用和批量删除数据(伪删除和直接删除) ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   ...

  6. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(17)-注册用户功能的细节处理(各种验证)

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(17)-注册用户功能的细节处理(各种验证) ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框 ...

  7. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(14)-主框架搭建

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(14)-主框架搭建    ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    (2 ...

  8. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(13)-权限设计

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(13)-权限设计 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    (2):数据 ...

  9. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(12)-实现用户异步登录和T4模板

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(12)-实现用户异步登录和T4模板 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建  ...

随机推荐

  1. Linux 系统级开启文件句柄 调优

    系统级开启文件句柄  max-file系统级别的能够打开的文件句柄的数量,Centos7默认是794168. Max-file 与 ulimit -n 的区别 max-file 表示系统级别的能够打开 ...

  2. 【题解】JSOIWC2019 Round1

    题面(T1变成5s(毒瘤出题人发现std超时了qaq)): 啥都不会qaq.但也送了不少分 题解: T1: 当T=0时直接异或前缀和,但T=1时就有点恶心 暴力能有80pts(防止大家爆零) 还珂以用 ...

  3. 如何退出vim

    按ESC键 按ESC键 按ESC键 然后: 最下面出现一条能输入命令的地方 输入冒号 输入冒号 输入冒号 然后输入命令: :w 保存文件但不退出 :w file 将修改另外保存到 file 中,不退出 ...

  4. C# 线程 正确使用Thread.Join()停止方式

    /// <summary>        /// 停下线程        /// </summary>        private void MyStopTask()     ...

  5. UVA1329 Corporative Network

    思路 用带权并查集维护到根的距离即可 代码 #include <cstdio> #include <algorithm> #include <cstring> #i ...

  6. Docker常用命令详解

    docker ps 查看当前正在运行的容器 docker ps -a 查看所有容器的状态 docker start/stop id/name 启动/停止某个容器 docker attach id 进入 ...

  7. 编码原则 之 Explicit Dependencies Principle

    Explicit Dependencies Principle The Explicit Dependencies Principle states: Methods and classes shou ...

  8. LINQ to Entities 不识别方法“System.String get_Item(Int32)”,因此该方法无法转换为存储表达式。

    1.LINQ to Entities 不识别方法“System.String get_Item(Int32)”,因此该方法无法转换为存储表达式.项目中发现linq to entities 不识别? , ...

  9. NodeJS:(二)基础常用API

    node.js中文网:http://nodejs.cn/api/ (path.Buffer.events.fs) ①path路径-----const {resolve} = require('path ...

  10. Thinkphph 使用RelationModel的三表关联查询机制

    有如下三个表 a表 b表 c表id bid other id cid other id other a表的bid关联b表的id,b表的cid关联c表的id 现在需要查询a表的时候顺带把b表和c表的相关 ...