java mvc ef_MVC+EF 入门教程(三)

一、前言

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

二、在 Aplication 定义服务

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

d0d18f541d86e647023a4460470bdd6e.png

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

7053945812255d7efc4eee359f6bb511.png

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

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

73e061ecadc6c76cca37839bdb9ba7a6.png

1effffc2c6a2e09b43c42fdd0dacd66c.png

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

86cd254359d9f8d1f73dbe46888c56b0.png

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

usingCore.Blogs;

usingEntityFrameworkDome.EFramework;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespaceApplication.Blogs

{

public classBlogServer

{

//获取 Blog 数据

public ListgetBlog()

{

var db = newSQLServerContext();

var data = db.Blogs.ToList();

returndata;

}

//删除 Blog 数据

public Boolean DeleteBlog(intid)

{

Boolean b = false;

var db = newSQLServerContext();

try{

Blog blog =db.Blogs.Find(id);

db.Blogs.Remove(blog);

db.SaveChanges();

b = true;

}

catch(Exception e) { }

returnb;

}

//编辑 Blog 数据

public Blog getEditBlog(intid)

{

Blog blog = null;

var db = newSQLServerContext();

blog =db.Blogs.Find(id);

returnblog;

}

//保存 Blog 数据

publicBoolean seveBlog(Blog blog)

{

Boolean b = false;

var db = newSQLServerContext();

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;

}

returnb;

}

//创建 Blog 数据

publicBoolean createNewBlog(Blog blog)

{

Boolean b = false;

var db = newSQLServerContext();

try{

db.Blogs.Add(blog);

db.SaveChanges();

b = true;

}

catch(Exception e)

{

throw;

}

returnb;

}

}

}

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

三、在 web 中页面实现

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

web / App_Start / RouteConfig

修改方式如下:

e4a57f66a9500442d9fe3d5f2211f818.png

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

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

将默认的控制器该为 BlogController

实例如下:

f72a9f2ab5f52e01f1d7ab686220d77f.png

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

26a0aafb3c9fc35f49a971e15c0a81ca.png

在BlogController 完成的代码如下:

usingApplication.Blogs;

usingCore.Blogs;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Web;

usingSystem.Web.Mvc;

namespaceWeb.Controllers

{

public classBlogController : Controller

{

// GET: Blog

public ActionResult Index(stringkey)

{

BlogServer db = newBlogServer();

List data =db.getBlog(key);

returnView(data);

}

publicActionResult ErrorPage()

{

returnView();

}

publicActionResult CreateBlog()

{

returnView();

}

[HttpPost]

public ActionResult CreateNewBlog(string Title, stringContect)

{

Blog b = newBlog();

b.Contect = Request.Form["Contect"];

b.Title = Request.Form["Title"];

b.CreatedTime =System.DateTime.Now;

BlogServer bs = newBlogServer();

if(bs.createNewBlog(b))

{

return RedirectToAction("Index");

}

return RedirectToAction("ErrorPage");

}

public ActionResult DeleteBlog(intid)

{

BlogServer db = newBlogServer();

if(db.DeleteBlog(id))

{

return RedirectToAction("Index");

}

return RedirectToAction("ErrorPage");

}

public ActionResult getEditBlog(intid)

{

BlogServer db = newBlogServer();

Blog blog =db.getEditBlog(id);

returnView(blog);

}

[HttpPost]

publicActionResult seveBlog(Blog b)

{

BlogServer db = newBlogServer();

if(db.seveBlog(b))

{

return RedirectToAction("Index");

}

return RedirectToAction("ErrorPage");

}

}

}

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

CreateBlog.cshtml

@{

ViewBag.Title = "CreateBlog";

}

//提交表单

functionSubmit() {

$("#frmRegist").submit();

}

//重置数据

functionReset() {

$("#frmReset").click(

function(){

$("#textTitle").val("");

$("#textContect").val("");

}

);

}

CreateBlog

Title

Contect

ErrorPage.cshtml

@{

ViewBag.Title = "ErrorPage";

}

找不到页面

getEditBlog.cshtml

@{

ViewBag.Title = "getEditBlog";

}

编辑Blog

functionSubmit() {

$("#frmSubmit1").submit();

}

Id

Title

Contect

CreatedTime

Reset

Index.cshtml

@using Core.Blogs;

Blog表单

IDTitleContectCreatedTime操作

{

@item.Id@item.Title@item.Contect@item.CreatedTime 修改 | 删除}

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

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

代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespaceSystem.Linq

{

public static classExtent

{

public static IQueryable WhereIf(this IQueryable source, System.Linq.Expressions.Expression> predicate, boolcondition)

{

return condition ?source.Where(predicate) : source;

}

public static IQueryable WhereIf(this IQueryable source, System.Linq.Expressions.Expression> predicate, boolcondition)

{

return condition ?source.Where(predicate) : source;

}

public static IEnumerable WhereIf(this IEnumerable source, Func predicate, boolcondition)

{

return condition ?source.Where(predicate) : source;

}

public static IEnumerable WhereIf(this IEnumerable source, Func predicate, boolcondition)

{

return condition ?source.Where(predicate) : source;

}

}

}

代码的位置在这里:

296485bd1275168d6eae13fb5b02df68.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值