添加Action DeleteUserInfo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplicationStudy.Models;
namespace MvcApplicationStudy.Controllers
{
public class UserInfoController : Controller
{
//
// GET: /UserInfo/ public ActionResult Index()
{
TestEntities db = new TestEntities();
var userInfoList = db.UserInfo.Where<UserInfo>(c => true);
List<UserInfo> list = userInfoList.ToList();
// ViewBag.Model = list;
//return View();
return View(list);
}
//展示一条数据详细信息
public ActionResult ShowDetail(int id)
{
TestEntities db = new TestEntities();
UserInfo userInfo = db.UserInfo.Where<UserInfo>(u => u.ID == id).SingleOrDefault();
if (userInfo != null)
{
return View(userInfo);
}
else
{
return Content("参数错误");
}
}
public ActionResult EditUserInfo(int id)
{
TestEntities db = new TestEntities();
UserInfo userInfo = db.UserInfo.Where<UserInfo>(u => u.ID == id).FirstOrDefault();
if (userInfo != null)
return View(userInfo);
else
return Content("参数错误");
}
[HttpPost]
public ActionResult EditUserInfo(UserInfo userInfo)
{
TestEntities db = new TestEntities();
db.Entry<UserInfo>(userInfo).State = System.Data.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
public ActionResult DeleteUserInfo(int id)
{
TestEntities db = new TestEntities();
UserInfo userInfo = db.UserInfo.Where<UserInfo>(u => u.ID == id).SingleOrDefault();
if (userInfo != null)
{
db.Entry<UserInfo>(userInfo).State = System.Data.EntityState.Deleted;
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return Content("删除失败");
}
} }
}

  修改Index视图如下:

@model IEnumerable<MvcApplicationStudy.Models.UserInfo>
@{
Layout = null;
} <!DOCTYPE html> <html>
<head> <meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function () {
var dels = $(".del");
for (var i = 0; i < dels.length; i++) {
dels[i].onclick = function () { return confirm("您确定要删除吗?");
};
} });
</script>
<title>Index</title>
</head>
<body>
<div>
<table>
<tr><th>编号</th><th>用户名</th><th>密码</th><th>时间</th><th>详细</th><th>删除</th><th>修改</th></tr>
@* @foreach(var userInfo in ViewBag.Model){
<tr>
<td>@userInfo.ID</td>
<td>@userInfo.UserName</td>
<td>@userInfo.UserPwd</td>
<td>@userInfo.RegTime</td>
<td>详细</td>
<td>删除</td>
<td>修改</td>
</tr> }*@
@foreach(var item in Model){
<tr>
<td>@Html.DisplayFor(modelItem=>item.ID)</td>
<td>@Html.DisplayFor(modelItem=>item.UserName)</td>
<td>@Html.DisplayFor(modelItem=>item.UserPwd)</td>
<td>@Html.DisplayFor(modelItem=>item.RegTime)</td>
<td>@Html.ActionLink("详细", "ShowDetail", new{ id=item.ID})</td>
<td>@Html.ActionLink("删除", "DeleteUserInfo", new { id = item.ID }, new { @class="del"})</td>
<td>@Html.ActionLink("修改", "EditUserInfo", new { id=item.ID})</td>
</tr>
}
</table>
</div>
</body>
</html>

  运行结果:

点击确定,该条记录就被删除了

MVC入门——删除页的更多相关文章

  1. MVC入门——列表页

    创建控制器UserInfoController using System; using System.Collections.Generic; using System.Linq; using Sys ...

  2. MVC入门——编辑页

    添加Action  EditUserInfo using System; using System.Collections.Generic; using System.Linq; using Syst ...

  3. MVC入门——详细页

    添加Action ShowDetail using System; using System.Collections.Generic; using System.Linq; using System. ...

  4. 26、ASP.NET MVC入门到精通——后台管理区域及分离、Js压缩、css、jquery扩展

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 有好一段时间没更新博文了,最近在忙两件事:1.看书,学习中...2.为公司年会节目做准备,由于许久没有练习双截棍了,难免生疏,所以现在临时抱 ...

  5. Asp.net MVC入门视频教程

    编程开发 > Asp.net视频教程 > Asp.net MVC入门视频教程 > 1.传统web处理方式和mvc处理方式 上传日期:2014-08-16 10:02:45  相关摘要 ...

  6. MVC入门教程

    MVC入门系列教程-视频版本,已入驻51CTO学院,文本+视频学效果更好哦.视频链接地址如下: 点我查看视频.另外,针对该系列教程博主提供有偿技术支持,群号:226090960,群内会针对该教程的问题 ...

  7. ASP.NET MVC 入门

    ASP.NET MVC 入门 (Learning ASP.NET MVC) 传统的WebForm发展到如今出现不少的缺陷, 比如为了解决Http的无状态WebForm模式使用了ViewsState来保 ...

  8. Java基础-SSM之Spring MVC入门篇

    Java基础-SSM之Spring MVC入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Spring MVC简介 1>.什么是Spring MVC 答:Sprin ...

  9. ASP.Net Core 2.2 MVC入门到基本使用系列 (四)

    本教程会对基本的.Net Core 进行一个大概的且不会太深入的讲解, 在您看完本系列之后, 能基本甚至熟练的使用.Net Core进行Web开发, 感受到.Net Core的魅力. 本教程知识点大体 ...

随机推荐

  1. scrapy之Selectors

    练习url:https://doc.scrapy.org/en/latest/_static/selectors-sample1.html 一 获取文本值 xpath In []: response. ...

  2. 文本生成器(bzoj 1030)

    Description JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版.该软件可以随机生成一些文章―――总是 ...

  3. Linux System Programming 学习笔记(四) 高级I/O

    1. Scatter/Gather I/O a single system call  to  read or write data between single data stream and mu ...

  4. 在AxureRP8中实现广告文字滚动效果

    本文是实现动态文字在一个区域中滚动的效果,大概实现过程如下: 先准备一个区域,然后让文字在该区域内水平移动,本文是实现了从右到左的轮询的效果,其他雷同. 在Axure中,这种移动的过程需要动态移动,利 ...

  5. Day 19 函数之闭包、装饰器

    一.什么是装饰器 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 二.装饰器遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰 ...

  6. SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)

    题目链接  LIS2 经典的三维偏序问题. 考虑$cdq$分治. 不过这题的顺序应该是 $cdq(l, mid)$ $solve(l, r)$ $cdq(mid+1, r)$ 因为有个$DP$. #i ...

  7. bzero和memset

    一. bzero和memset函数 1. bzero已不建议使用 原型:extern void bzero(void *s, int n); 2.memset void *memset(void *s ...

  8. luogu P2056 采花

    题目描述 萧芸斓是 Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了 n 朵花,花有 c 种颜色(用整数 1-c 表示) ,且花是排成 ...

  9. SilverLight:布局(3)StackPanel 对象

    ylbtech-SilverLight-Layout: 布局(3)StackPanel 对象 A, Nesting Layout Containers(内嵌布局容器) B, StackPanel(队列 ...

  10. .NET组件编程

    链接 http://www.cnblogs.com/mapserver/archive/2006/03/06/343632.html