mvc mysql linq_【C#】MVC+EF+LINQ 综合小项目

第一,创建数据库

6bac7584d5b5aa69ea27c9e759bb3973.png

create table category(

id int primary key,

name nvarchar(20)

)

create table news(

id int primary key,

title nvarchar(20),

content nvarchar(200),

createTime datetime ,

caid int constraint fk_ca_id foreign key references category(id)

)

create table comment(

id int primary key,

content nvarchar(100),

createTime dateTime,

userIp nvarchar(50),

newsId int constraint fk_news_id foreign key references news(id)

)

手动添加数据

07b7be4bd8b289850ac7df15acc9fac8.png

第二,创建MVC工程文件

aeab9564dd77dbf6ed6883e6313dd5d0.png

第三,在Models文件夹下创建 EF

930e7c8094b0faba6264c543b808225c.png

49d87f6afc4b55d09fcabcf527dec652.png

第四,修改3个页面

1.

d88451ba40312ea8d79b049a65852261.png

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;usingMVCBlog.Models;usingSystem.Data;namespaceMVCBlog.Controllers

{public classHomeController : Controller

{///

///数据上下文对象///

BlogDBEntities db = newBlogDBEntities();#region 查询新闻列表+ActionResult Index()

///

///查询新闻列表///

///

publicActionResult Index()

{//1.查询数据库里的新闻数据(通过EF执行)//1.1第一种方法:使用SQO(标准查询运算符),查询所有新闻//db.news.Where(d =>d.id !=0 );//List list = db.news.Where(d => d.id != 0).ToList ();//1.2第二种方法:使用linq语句,查询所有新闻标题//Linq仅仅是给程序员使用的语法糖,.net编译器会在编译时将linq转化为sqo

List list = (from d in db.news where d.id != 0 selectd).ToList();//2.将数据集合传给视图

ViewData["datalist"] =list;//3.加载视图

returnView();

}#endregion

#region 执行删除操作(根据id)+ActionResult Del(int id)

///

///执行删除操作(根据id)///

/// 要删除的新闻id

///

public ActionResult Del(intid)

{try{//1.创建要删除的对象

news modelDel = new news() { id =id };//2.将对象添加到EF管理容器中

db.news.Attach(modelDel);//3.将对象包装类的状态标识为删除状态

db.news.Remove(modelDel);//4.更新到数据库

db.SaveChanges();//5.更新成功,则令浏览器跳转到list方法

return RedirectToAction("Index", "Home");

}catch(Exception ex)

{return Content("删除失败!" +ex.Message);

}

}#endregion

#region 显示要修改的数据(根据id)+ActionResult Modify(int id)[HttpGet]///

///执行修改操作(根据id)///

/// 要修改的新闻id

///

public ActionResult Modify(intid)

{try{//根据id查询数据库,返回集合中,拿到第一个实体对象

news n = (from a in db.news where a.id == id selecta).FirstOrDefault();//生成分类下拉框列表集合List list

IEnumerable listitem = (from c in db.categories select c).ToList().Select(c => new SelectListItem { Value = c.id.ToString(), Text =c.name });

ViewBag.CateList=listitem;//将n传递给视图显示 viewbag 或者viewdata//加载视图,使用view的构造函数 将数据传给视图上的名为model的属性

returnView(n);

}catch(Exception ex)

{return Content("修改失败!" +ex.Message);

}

}#endregion

#region 执行修改操作+ActionResult Modify(news model)[HttpPost]///

///执行修改操作///

///

///

publicActionResult Modify(news model)

{try{//将实体对象加入到EF对象容器中,并获取伪包装类对象

var entry = db.Entry(model);//将包装类对象的状态设置为unchanged

entry.State =System.Data.Entity.EntityState.Unchanged;//设置需要提交的实体属性

entry.Property(a => a.title).IsModified = true;

entry.Property(a=> a.content).IsModified = true;//提交到数据库 完成修改

db.SaveChanges();//5.更新成功,则令浏览器跳转到list方法

return RedirectToAction("Index", "Home");

}catch(Exception ex)

{return Content("修改失败!" +ex.Message);

}

}#endregion}

}

2.

400a1d23c32b49c9c2128beacaed81b5.png

@using MVCBlog.Models

@{

Layout= null;

}

Index

border: 1px solid #0ff;

width: 800px;

margin: 10px auto;

border-collapse: collapse;

}

#tblist th, td {

border: 1px solid #0ff;

padding: 10px;

}

alert("运行到了这里");if (confirm("您确定要删除吗?亲~~")) {

window.location= "/Home/Del/" +id;

}

}

function Modify(id) {

window.location= "/Home/Modify/" +id;

}

id标题发布时间新闻分类操作

@foreach (news nin ViewData["datalist"] as List)

{

@n.id @n.title @n.createTime @n.caid

删除

修改

}

3.

471e235f320195a4bdb555ab2d70207b.png

@model MVCBlog.Models.news

@{

Layout= null;

}

修改

border: 1px solid #0ff;

width: 600px;

margin: 10px auto;

border-collapse: collapse;

}

#tblist th, td {

border: 1px solid #0ff;

padding: 10px;

}

{

修改 @Html.HiddenFor(a => a.id)
标题:@Html.TextBox("txtName",(object )Model.title)@Html.TextBoxFor(a => a.title)
分类:@Html.DropDownListFor(a => a.category, ViewBag.CateList as IEnumerable)
内容:@Html.TextAreaFor(a => a.content, 10, 60, null)
@Html.ActionLink("返回", "Index", "Home")
}

第四,页面运行实现

af72605b2c4f37dfd0abb7e1fe874278.png

c67d16dd44a466117fa4d772425e10f5.png

第五,BUG   将List修改为去取两个表关联的数据,前后台的修改。

12e933dff4aad1e243d5a65d4a26c1ed.png

69228fe3dbca7d813c4e2673bfcb7bb9.png

代码下载地址:             链接:https://pan.baidu.com/s/1_4NKrIqM51EPOMJLSWSB0Q

提取码:wpjc

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值