MVC入门——删除页
添加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入门——删除页的更多相关文章
- MVC入门——列表页
创建控制器UserInfoController using System; using System.Collections.Generic; using System.Linq; using Sys ...
- MVC入门——编辑页
添加Action EditUserInfo using System; using System.Collections.Generic; using System.Linq; using Syst ...
- MVC入门——详细页
添加Action ShowDetail using System; using System.Collections.Generic; using System.Linq; using System. ...
- 26、ASP.NET MVC入门到精通——后台管理区域及分离、Js压缩、css、jquery扩展
本系列目录:ASP.NET MVC4入门到精通系列目录汇总 有好一段时间没更新博文了,最近在忙两件事:1.看书,学习中...2.为公司年会节目做准备,由于许久没有练习双截棍了,难免生疏,所以现在临时抱 ...
- Asp.net MVC入门视频教程
编程开发 > Asp.net视频教程 > Asp.net MVC入门视频教程 > 1.传统web处理方式和mvc处理方式 上传日期:2014-08-16 10:02:45 相关摘要 ...
- MVC入门教程
MVC入门系列教程-视频版本,已入驻51CTO学院,文本+视频学效果更好哦.视频链接地址如下: 点我查看视频.另外,针对该系列教程博主提供有偿技术支持,群号:226090960,群内会针对该教程的问题 ...
- ASP.NET MVC 入门
ASP.NET MVC 入门 (Learning ASP.NET MVC) 传统的WebForm发展到如今出现不少的缺陷, 比如为了解决Http的无状态WebForm模式使用了ViewsState来保 ...
- Java基础-SSM之Spring MVC入门篇
Java基础-SSM之Spring MVC入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Spring MVC简介 1>.什么是Spring MVC 答:Sprin ...
- ASP.Net Core 2.2 MVC入门到基本使用系列 (四)
本教程会对基本的.Net Core 进行一个大概的且不会太深入的讲解, 在您看完本系列之后, 能基本甚至熟练的使用.Net Core进行Web开发, 感受到.Net Core的魅力. 本教程知识点大体 ...
随机推荐
- Unity Microphone 录音时 检测声音大小
刚开始以为只取录音时的最后一个sample来判断音量大小,发现都检测不到. 后来搜索了一下,原来需要取一段sample来判断,有的是这一段取平均值作为音量大小.我这里是取出一段sample中的峰值(p ...
- C语言第五题
学生成绩管理系统 一.登录页面分为:首页展示学生登录和老师登录让用户选择,选择完以后提示用户输入账户名和密码 ps:1.老师的账户目前就一个,账户密码可以设置成admin/admin 2.学生的账户是 ...
- [LeetCode] Text Justification words显示的排序控制
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- configure.ac:3: error: Autoconf version 2.68 or higher is required
configure.ac:3: error: Autoconf version 2.68 or higher is required 参考博客:https://blog.csdn.net/pretty ...
- Spring Boot中实现异步调用之@Async
一.什么是异步调用 “异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行:异步调用指程序在顺序执行时,不等待异步调用 的语句返回结果 ...
- 获取path的几种方式:NSFileManager NSHomeDirectory NSBundle
//---------------------------------------------------------------------------------NSFileManager *fi ...
- openfire Android学习(一)----实现用户注册、登录、修改密码和注销等
以前学习过用Scoket 建立聊天,简单的建立聊天是没问题的,但如果要实现多人复杂的聊天,后台服务器代码就比较复杂,对于我这新手来讲就比较难了.后来在网上看到用openfire做服务器,利用强大的Sm ...
- [剑指Offer]2.变态跳台阶
题目 一仅仅青蛙一次能够跳上1级台阶,也能够跳上2级--它也能够跳上n级. 求该青蛙跳上一个n级的台阶总共同拥有多少种跳法. 思路 用Fib(n)表示青蛙跳上n阶台阶的跳法数,设定Fib(0) = 1 ...
- 3D空间中射线与轴向包围盒AABB的交叉检测算法 【转】
http://blog.csdn.net/i_dovelemon/article/details/38342739 引言 在上一节中,我讲述了如何实现射线与三角形的交叉检测算法. 但是,我们应该知道, ...
- NYOJ 353 3D dungeon 【bfs】
题意:给你一个高L长R宽C的图形.每个坐标都能够视为一个方格.你一次能够向上.下.左,右,前,后任一方向移动一个方格, 可是不能向有#标记的方格移动. 问:从S出发能不能到达E,假设能请输出最少的移动 ...