一、前言

    上一节,我们将了生成数据库,那么这张我就将操作设计库。

二、在 Aplication 定义服务

  在 Application 中添加文件夹(Blog)和 操作类(BlogServer)。实例如下:

  

  结果有报错,提示是如下:

  

  那么我们的解决方案是:在 Application 中也加入 EntityFramework 的程序集。

  在找到 引用 -->管理NuGet重新包 实例如下:

   

  

  然后安装它,代码就不报错了。

  

  实现对 Blog 的 CRUD 的代码如下:

using Core.Blogs;
using EntityFrameworkDome.EFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Application.Blogs
{
public class BlogServer
{
//获取 Blog 数据
public List<Blog> getBlog()
{
var db = new SQLServerContext();
var data = db.Blogs.ToList<Blog>(); return data;
} //删除 Blog 数据
public Boolean DeleteBlog(int id)
{
Boolean b = false;
var db = new SQLServerContext();
try
{
Blog blog = db.Blogs.Find(id);
db.Blogs.Remove(blog);
db.SaveChanges();
b = true;
}
catch (Exception e) { }
return b;
} //编辑 Blog 数据
public Blog getEditBlog(int id)
{
Blog blog = null;
var db = new SQLServerContext();
blog = db.Blogs.Find(id);
return blog;
} //保存 Blog 数据
public Boolean seveBlog(Blog blog)
{
Boolean b = false;
var db = new SQLServerContext();
try
{
Blog a = db.Blogs.First(r => r.Id == blog.Id);
a.Contect = blog.Contect;
a.Title = blog.Title;
a.CreatedTime = blog.CreatedTime;
db.SaveChanges();
b = true;
}
catch (Exception)
{ throw;
} return b;
} //创建 Blog 数据
public Boolean createNewBlog(Blog blog)
{
Boolean b = false;
var db = new SQLServerContext();
try
{
db.Blogs.Add(blog);
db.SaveChanges();
b = true;
}
catch (Exception e)
{
throw;
}
return b;
}
}
}

  服务代码也编写完成,那么我们开始操作服务代码。

三、在 web 中页面实现

  在 web 中,首先我们去修改路由,路由在什么地方?这个很好找

  web / App_Start / RouteConfig

  修改方式如下:

  

  然后再 Controller 中,新建 BlogController 控制器。

  具体添加方式:Controllers  --> 添加 --> 控制器 --> MVC 5 控制器 - 空

  将默认的控制器该为 BlogController

  实例如下:

  

  然后在 Index 作用域 中,右键添加 -->添加视图

  

  在BlogController 完成的代码如下:

  

using Application.Blogs;
using Core.Blogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Web.Controllers
{
public class BlogController : Controller
{
// GET: Blog
public ActionResult Index(string key)
{
BlogServer db = new BlogServer();
List<Blog> data = db.getBlog(key);
return View(data);
} public ActionResult ErrorPage()
{ return View();
}
public ActionResult CreateBlog()
{
return View();
}
[HttpPost]
public ActionResult CreateNewBlog(string Title, string Contect)
{ Blog b = new Blog();
b.Contect = Request.Form["Contect"];
b.Title = Request.Form["Title"];
b.CreatedTime = System.DateTime.Now;
BlogServer bs = new BlogServer();
if (bs.createNewBlog(b))
{
return RedirectToAction("Index");
}
return RedirectToAction("ErrorPage"); } public ActionResult DeleteBlog(int id)
{
BlogServer db = new BlogServer(); if (db.DeleteBlog(id))
{
return RedirectToAction("Index");
}
return RedirectToAction("ErrorPage");
} public ActionResult getEditBlog(int id)
{
BlogServer db = new BlogServer();
Blog blog = db.getEditBlog(id);
return View(blog);
} [HttpPost]
public ActionResult seveBlog(Blog b)
{
BlogServer db = new BlogServer();
if (db.seveBlog(b))
{
return RedirectToAction("Index");
}
return RedirectToAction("ErrorPage");
} }
}

  在BlogController 中涉及到的视图如下:

  CreateBlog.cshtml

@{
ViewBag.Title = "CreateBlog";
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript"> //提交表单
function Submit() {
$("#frmRegist").submit();
} //重置数据
function Reset() {
$("#frmReset").click(
function(){
$("#textTitle").val("");
$("#textContect").val("");
}
);
}
</script> <h2>CreateBlog</h2>
<form id="frmRegist" method="post" action="CreateNewBlog">
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<input type="text" class="form-control" placeholder="Title" name="Title" id="textTitle">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Contect</label>
<input type="text" class="form-control" placeholder="Contect" name="Contect" id="textContect">
</div>
<input type="reset" id="frmReset" value="reset" onclick="Reset()">
<input type="button" id="frmSubmit" value="Submit" onclick="Submit()">
</form>

  ErrorPage.cshtml

  

@{
ViewBag.Title = "ErrorPage";
} <h2>找不到页面</h2>

  getEditBlog.cshtml

  

@{
ViewBag.Title = "getEditBlog";
} <h2>编辑Blog</h2>
<script src="~/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript">
function Submit() {
$("#frmSubmit1").submit();
}
</script> <form id="frmSubmit1" method="post" action="seveBlog">
<div class="form-group">
<label>Id</label>
<input type="text" class="form-control" value="@Model.Id" name="Id">
</div>
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" value="@Model.Title" name="Title">
</div>
<div class="form-group">
<label>Contect</label>
<input type="text" class="form-control" value="@Model.Contect" name="Contect">
</div>
<div class="form-group">
<label>CreatedTime</label>
<input type="datetime" class="form-control" value="@Model.CreatedTime" name="CreatedTime">
</div>
<button type="reset" class="btn btn-default">Reset</button>
<input type="button" value="Submit" onclick="Submit()">
</form>

  Index.cshtml

  

@using Core.Blogs;
<h2>Blog表单</h2>
<div style="width:100%; height:20px; ">
<span style="float:right;"><a href="Blog/CreateBlog">创建Blog</a></span>
</div> <!--用Requert 只能获取Get的参数-->
<form method="get" action="/Blog/Index">
<input type="text" name="key" value="@Request.QueryString["key"]" placeholder="查询" />
<input type="submit" value="查询" />
</form>
<table border="1" width="100%" cellpadding="0" cellspacing="10">
<tr>
<th>ID</th>
<th>Title</th>
<th>Contect</th>
<th>CreatedTime</th>
<th>操作</th>
</tr>
@foreach (Blog item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.Title</td>
<td>@item.Contect</td>
<td>@item.CreatedTime</td>
<td><a href="Blog/getEditBlog?id=@item.Id">修改</a> | <a href="Blog/DeleteBlog?id=@item.Id">删除</a></td>
</tr>
} </table>

  实现了你会发现,运行不起来,原因是你还需要添加 EntityFramework 程序集。

  同时在查询的getBlog()  的时候,使用的 WhereIf 会报错,原因是EF中没有包含,所以我们自己写了一个类了对它进行泛化。

  代码如下:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace System.Linq
{
public static class Extent
{
public static IQueryable<T> WhereIf<T>(this IQueryable<T> source, System.Linq.Expressions.Expression<Func<T, bool>> predicate, bool condition)
{
return condition ? source.Where(predicate) : source;
}
public static IQueryable<T> WhereIf<T>(this IQueryable<T> source, System.Linq.Expressions.Expression<Func<T, int, bool>> predicate, bool condition)
{
return condition ? source.Where(predicate) : source;
}
public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, Func<T, bool> predicate, bool condition)
{
return condition ? source.Where(predicate) : source;
}
public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, Func<T, int, bool> predicate, bool condition)
{
return condition ? source.Where(predicate) : source;
}
}
}

代码的位置在这里:

  

  慢慢的你会发现很多的好东西,如 为什么要回填查询的Key?是怎么样实现?

  

MVC+EF 入门教程(三)的更多相关文章

  1. MVC+EF 入门教程(一)

    一.前言 本人小白,写这篇博客是为了记录今天一天所学的知识,可能表达不是那么的准确和清楚,欢迎在留言区写下您的建议,欢迎纠错.希望这篇文章对你有所帮助学习 .Net MVC有所帮助.废话不多说了,我们 ...

  2. MVC+EF 入门教程(二)

    一.前沿 为了使以后项目分开,所以我会添加3个类库.用于存储 实体.数据库迁移.服务.这种思路是源于我使用的一个框架 ABP.有兴趣的您,可以去研究和使用这个框架. 二.修改本地连接 在项目中,找到  ...

  3. mvc+ef入门(三)

    (1)新建一个DAL层用来放置Accountcontext.cs和Accountinitializer.新建一个models层用来归放sysuser,sysrole和sysuserrole,三个类.( ...

  4. MVC+EF 入门教程(四)

    一.前言 写了那么久,那么现在给大家看效果了 二.效果展示 点击创建Blog 显示 编辑 编辑成功,是不是很酷. 删除 终于完成了,准备睡觉!虽然有很多不足的地方,我会慢慢的去改的.感谢累了一天的自己 ...

  5. ASP.NET MVC 5 入门教程 (2) 控制器Controller

    文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-controller.html 上一节:ASP.NET MVC ...

  6. ASP.NET MVC 5 入门教程 (4) View和ViewBag

    文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-view.html 上一节:ASP.NET MVC 5 入门教 ...

  7. 无废话ExtJs 入门教程三[窗体:Window组件]

    无废话ExtJs 入门教程三[窗体:Window组件] extjs技术交流,欢迎加群(201926085) 1.代码如下: 1 <!DOCTYPE html PUBLIC "-//W3 ...

  8. ASP.NET MVC 5 入门教程 (3) 路由route

    文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-route.html 上一节:ASP.NET MVC 5 入门 ...

  9. ASP.NET MVC 5 入门教程 (1) 新建项目

    文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-create-project.html 下一节:ASP.NET ...

随机推荐

  1. SSD中的GC机制以及Trim

    GC(Garbagecollection)垃圾回收  所谓GC就是把一个闪存块里的"有效"页数据复制到一个"空白"块里,然后把这个块完全擦除.GC是 SSD里的 ...

  2. 基于FPGA的有限状态机浅析

    前言:状态机大法好,状态机几乎可以实现一切时序逻辑电路. 有限状态机(Finite State Machine, FSM),根据状态机的输出是否与输入有关,可分为Moore型状态机和Mealy型状态机 ...

  3. Linux指令 vi编辑,保存及退出

    编辑模式 使用vi进入文本后,按i开始编辑文本退出编辑模式 按ESC键,然后: 退出vi :q! 不保存文件,强制退出vi命令 :w 保存文件,不退出vi命令 :wq 保存文件,退出vi命令 中断vi ...

  4. 关于 use-default-filters 属性的说明

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7940755.html ------------------------------------ ...

  5. Unity20172.0 Android平台打包

    Android SDK及Jdk百度网盘下载链接:https://pan.baidu.com/s/1dFbEmdz 密码:pt7b Unity20172.0 Android平台打包 简介说明: 第一步: ...

  6. Java第三季

    1.异常简介: (1) Java中的所有不正常类都继承于Throwable类.Throwable主要包括两个大类,一个是Error类,另一个是 Exception类: (2)其中Error类中包括虚拟 ...

  7. 解释器模式(Interpreter)

    解释器模式(Interpreter)解释器模式是我们暂时的最后一讲,一般主要应用在OOP开发中的编译器的开发中,所以适用面比较窄. Context类是一个上下文环境类,Plus和Minus分别是用来计 ...

  8. Web应用与应用层协议

    Web应用与应用层协议 本篇博文中的主要参考文献是<计算机网络高级教程>,分别是吴功宜老先生和吴英教授合著.这部教程是我研究生老师所推荐的网络必读科目,由于该教程讲解的基础知识详细,但内容 ...

  9. 实践作业1:测试管理工具实践 Day1

    1.熟悉课程平台2.选取小组作业工具并分工3.申请博客4.提交<高级软件测试技术SPOC2017年秋学生博客地址汇总>问卷5.着手熟悉Testlink

  10. Elasticsearch中Head插件的使用

    在学习Elasticsearch的过程中,必不可少需要通过一些工具查看es的运行状态以及数据.如果都是通过rest请求,未免太过麻烦,而且也不够人性化.此时,head可以完美的帮助你快速学习和使用El ...