首页 技术 正文
技术 2022年11月14日
0 收藏 749 点赞 2,355 浏览 2159 个字

以下的例子以留言本作为依据。

1.添加

        public ActionResult Create()
{
return View();
} //
// POST: /Contact/Create [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Message message)
{
if (ModelState.IsValid)
{
db.Messages.Add(message);
db.SaveChanges();
return RedirectToAction("Index");
} return View(message);
}

2.查看

 public ActionResult Index()
{
return View(db.Messages.ToList());
}

3.全部更新

//
// GET: /Contact/Edit/5 public ActionResult Edit(int id = )
{
Message message = db.Messages.Find(id);
if (message == null)
{
return HttpNotFound();
}
return View(message);
} //
// POST: /Contact/Edit/5 [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Message message)
{
if (ModelState.IsValid)
{
db.Entry(message).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(message);
}

4.删除 

        //
// GET: /Contact/Delete/5 public ActionResult Delete(int id = )
{
Message message = db.Messages.Find(id);
if (message == null)
{
return HttpNotFound();
}
return View(message);
} //
// POST: /Contact/Delete/5 [HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Message message = db.Messages.Find(id);
db.Messages.Remove(message);
db.SaveChanges();
return RedirectToAction("Index");
}

5.释放空间

protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}

6.部分更新记录

有的时候并不需要把一条数据记录全部更新,这时候必须注意编辑数据提交到数据库,会不会让数据库已有数据丢失。下面讨论避免的三种办法。

(1)在表单中利用HiddenFor把值传递到控制器动作中

...
@using(Html.BeginForm())
{
  ....
@Html.HiddenFor(a=>a.Id)
}

(2)另一种是在控制器中补充完整数据库中的数据。

        [HttpPost]
public ActionResult TestForm(Guestbook guestbook)
{
if (ModelState.IsValid)
{
var gb = db.Guestbooks.Find();
gb.confirm=guestbook.confirm;
gb.replied=guestbook.replied;
....
db.SaveChanges();
return Redirect("/");
}
return View();
}

(3)TryUpdateModel

       [HttpPost]
public ActionResult TestForm(FormCollection form)
{
if (ModelState.IsValid)
{
var gb = db.Guestbooks.Find();
TryUpdateModel(gb, "", form.AllKeys);
db.SaveChanges();
return Redirect("/");
}
return View();
}

  关于TryUpdateModel的用法,还可以是TryUpdateModel(model, new string[] { “字段1”, “字段2”, “字段3” }),以及设置排除form表单接收进来的某些字段TryUpdateModel(model, “”, FromValue.AllKeys, new string[] { “字段1” })。

7.注销

        public ActionResult LogOff()
{
//清除窗体验证的Cookies
FormsAuthentication.SignOut(); //清除所有曾经写入过的Session信息
Session.Clear(); return RedirectToAction("Login", "Account");
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295