添加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. Codevs 数字三角形 问题合集

    1220 数字三角形 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或得 ...

  2. 标准C程序设计七---117

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  3. 《Programming in C》读书笔记

    该书由美国Seephen G.Kochan著 贾洪峰译,电子工业出版社,来源是九江学院图书馆采购,现在藏于九江学院图书馆逸夫楼. 本书的主要内容: 第一章.基础知识 第二章.编译和运行第一个程序 第三 ...

  4. C#实现键盘钩子

    前言: 因为项目中需要使用到快捷键,所以上网找资料了解关于快捷键的实现技术,于是有了键盘钩子的使用学习.在网上了解到,键盘钩子其实只是很多种钩子中的其中一种.所谓钩子:请看下面关于钩子的描述(来自百度 ...

  5. PHP实现15位身份证号转18位

    PHP实现15位身份证号转18位 参考博客: 作者:selfimpr626 来源:CSDN (根据身份证号计算年龄,15位身份证号码转18位) 原文:https://blog.csdn.net/wei ...

  6. "从客户端中检测到有潜在危险的 Request.Form 值"的解决方案汇总

    #事故现场 在一个asp.net 的项目中,前端通过ajax将富文本中的文字内容post到服务端的一个ashx中,在ashx中尝试读取参数值时, 结果报错:“从客户端中检测到有潜在危险的 Reques ...

  7. Web地图服务、WMS 请求方式、网络地图服务(WMS)的三大操作

    转自奔跑的熊猫原文 Web地图服务.WMS 请求方式.网络地图服务(WMS)的三大操作 1.GeoServer(地理信息系统服务器) GeoServer是OpenGIS Web 服务器规范的 J2EE ...

  8. git学习五:eclipse使用git下载项目

    原文:http://blog.csdn.net/u014079773/article/details/51597213 准备工作: 目的:从远程仓库github上down所需的项目 eclipse使用 ...

  9. 几点iOS开发技巧

    转自I'm Allen的博客   原文:iOS Programming Architecture and Design Guidelines   原文来自破船的分享   原文作者是开发界中知晓度相当高 ...

  10. 邁向IT專家成功之路的三十則鐵律 鐵律二十一:IT人用才之道-穿透

    在以道德為基礎的企業主管之人,其最根本的能力除了須要有洞悉事物的敏捷思維之外,眼光還必要有像水柱般一樣的穿山引石之能,如此不僅能夠為企業找到適才之人,更能為企業的永續經營奠定有如泰山般的基石.只可惜大 ...