MVC简单实例




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models; namespace WebApplication1.Controllers
{
public class LeaveMsgController:Controller
{
//GET:/LeaveMsg/
public ActionResult Index()
{
//Controllers向视图传值
//原理:先存数据然后再在视图你面取出来
string s = "hello!!";
ViewData["aa"] = s; BBSDataContext bbs = new BBSDataContext();
List<student> v = (from m in bbs.student
select m).ToList();
ViewData["list"] = v; return View();
}
//删除
public ActionResult Delete()
{
//RouterData 可以获取路由上的数据
string id = RouteData.Values["id"].ToString();
//可以将一段文字返回给浏览器
//return view() 是把当前这个Action对应的视图返回给浏览器
BBSDataContext dc = new BBSDataContext();
var v = from d in dc.student
where d.stuID == int.Parse(id)
select d;
if (v.Count()>)
{
//删除
try
{
dc.student.DeleteOnSubmit(v.First());
dc.SubmitChanges();
//成功
//TempData临时数据,为下一次请求服务,在下一次请求完毕,数据清除
//本质是session存放数据
TempData["msg"] = "删除成功";
}
catch (Exception)
{
//失败
TempData["msg"] = "删除失败!!!";
} }
else
{
//没有对应的数据
TempData["msg"] = "没有找到对应的数据!!!";
}
return RedirectToAction("Index");
} //新增
public ActionResult Insert()
{
return View(); } //数据验证
public void CheckData(student s)
{
//用户名不能为空
if (s.stuName=="")
{
ModelState.AddModelError("stuName", "用户名不能为空");
}
//性别内容长度不能为空且不能大于2个字符
if (s.stuNo==""&&s.stuNo.Length<)
{
ModelState.AddModelError("stuName", "用户名不能为空或字符数大于2");
}
//正则表达式
//bool b= Regex.IsMatch(s.stuID.ToString(), "^\\{6}$");//C#正则 }
//提交新增操作
public ActionResult InsertOK()
{
//数据验证 BBSDataContext dc = new BBSDataContext();
student s = new student();
s.stuName = Request["stuName"];
s.stuID = int.Parse(Request["stuNo"]);
s.sex = Request["sex"];
s.remark = Request["remark"];
CheckData(s);
if (!ModelState.IsValid)//验证不通过
{
ViewData["stuName"] = s.stuName;
ViewData["stuNo"] = s.stuNo;
ViewData["sex"] = s.sex;
return View("Insert");
}
try
{
dc.student.InsertOnSubmit(s);
dc.SubmitChanges();
TempData["msg"] = "新增成功!!!";
return RedirectToAction("Index");
}
catch (Exception)
{
TempData["msg"] = "新增失败";
return RedirectToAction("Insert");
}
} //编辑跳转
public ActionResult Edit()
{ string id = RouteData.Values["id"].ToString();
ViewData["id"] = id;
//找id对应的数据
BBSDataContext dc = new BBSDataContext();
var v = from d in dc.student
where d.stuID == int.Parse(id)
select d;
if (v.Count()>)
{
var d = v.First();
ViewData["stuName"] = d.stuName;
ViewData["stuNo"] = d.stuNo;
ViewData["sex"] = d.sex;
ViewData["remark"] = d.remark;
return View();
}
else
{
TempData["msg"] = "没有对应的数据";
return View();
}
} //修改
public ActionResult Update()
{
string id = Request["id"];
BBSDataContext dc = new BBSDataContext();
var v = from d in dc.student
where d.stuID == int.Parse(id)
select d;
if (v.Count()>)
{
var m = v.First();
m.stuName = Request["stuName"];
m.stuNo = Request["stuNo"];
m.sex = Request["sex"];
CheckData(m);
if (!ModelState.IsValid)
{
return View("Edit");
}
try
{
dc.SubmitChanges();//提交修改
TempData["msg"] = "修改成功";
return RedirectToAction("Index");
}
catch (Exception)
{ TempData["msg"] = "修改失败!";
return RedirectToAction("Edit",new { id=id});
} }
else
{
//没有此数据
TempData["msg"] = "提交数据之前数据被其他用户已删除!!";
return RedirectToAction("Index");
}
} }
}
控制器中业务逻辑
@{
ViewBag.Title = "Index";
}
@using WebApplication1.Models
<h2>学生列表</h2>
@ViewData["aa"]
@TempData["msg"]
<table width="100%">
<tr><th>姓名</th><th>学号</th><th>性别</th><th>操作</th></tr>
@{
List<student> list = ViewData["list"] as List<student>;
}
@{
foreach (var item in list)
{
<tr>
<td>@item.stuName</td>
<td>@item.stuID</td>
<td>@item.sex</td>
<td>@* 在MVC里提供了2个helper类,htmlHelper,UrlHelper类可以生成html代码和url代码,使用比较方便
<a href='student/delete'/@item.stuID>删除</a>
@Url.Action("delete", "student", new {id=item.stuID})*@
@Html.ActionLink("编辑", "Edit", new {id=item.stuID})|
@Html.ActionLink("删除", "Delete", new { id = item.stuID })
</td>
</tr>
}
}
</table>
@Html.ActionLink("新增","Insert")
Index页面
@{
ViewBag.Title = "Insert";
}
<h2>新增页面</h2>
@TempData["msg"]
<p>
@Html.ValidationSummary();
</p>
@{
using (Html.BeginForm("InsertOK","LeaveMsg", FormMethod.Post))
{//创建的对象会在using语句块结束时关闭对象
<span>用户名</span> @Html.TextBox("stuName")<br />
<span>学号</span> @Html.TextBox("stuNo")<br />
<span>性别</span> @Html.TextBox("sex")<br />
<span>评论</span> @Html.TextArea("remark",null,5,30,null)<br />
<input type="submit" value="新增" />
@Html.ActionLink("返回列表", "Index");
}
}
新增页面
@{
ViewBag.Title = "Edit";
}
<h2>编辑信息</h2>
@TempData["msg"]
<p>
@Html.ValidationSummary();
</p>
@{
using (Html.BeginForm("Update", "LeaveMsg", FormMethod.Post))
{
@Html.Hidden("id")
<span>用户名</span> @Html.TextBox("stuName")<br />
<span>学号</span> @Html.TextBox("stuNo")<br />
<span>性别</span> @Html.TextBox("sex")<br />
<span>评论</span> @Html.TextArea("remark", null, 5, 30, null)<br />
<input type="submit" value="修改" />
@Html.ActionLink("返回列表", "Index");
}
}
编辑页面
MVC简单实例的更多相关文章
- Maven+Eclipse+Spring MVC简单实例
1. ToolsVersion and Preparations: Eclipse: 3.5 (eclipse-jee-galileo-win32) Maven: 2.0.11 Spring MVC ...
- PHP: 手把手编写自己的 MVC 框架实例教程
1 什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...
- 简单实例一步一步帮你搞清楚MVC3中的路由以及区域
我们都知道MVC 3 程序的所有请求都是先经过路由解析然后分配到特定的Controller 以及 Action 中的,为什么这些知识讲完了Controller Action Model 后再讲呢?这个 ...
- ASP.NET MVC 简单介绍①
ASP.NET MVC 简单介绍① 只做了重要描述,内容出自菜鸟教程网站内容. 目录 1布局 2HTML 帮助器 3.Razor 语法 4.添加样式 5.Layout 6. Controllers ...
- SpringMVC笔记——SSM框架搭建简单实例
落叶枫桥 博客园 首页 新随笔 联系 订阅 管理 SpringMVC笔记——SSM框架搭建简单实例 简介 Spring+SpringMVC+MyBatis框架(SSM)是比较热门的中小型企业级项目开发 ...
- SpringMVC简单实例(看起来有用)
SpringMVC简单实例(看起来有用) 参考: SpringMVC 基础教程 简单入门实例 - CSDN博客http://blog.csdn.net/swingpyzf/article/detail ...
- ThinkPHP5.0最最最最最简单实例
ThinkPHP5.0最最最最最简单实例 一.效果图 二.操作步骤 1.用mysql数据库建立数据库 2.按照ThinkPHP官网的指示装好ThinkPHP5.0 tp5里面的目录结构如下: 3.配置 ...
- Spring boot项目搭建及简单实例
Spring boot项目搭建 Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for ...
- Hibernate(二)__简单实例入门
首先我们进一步理解什么是对象关系映射模型? 它将对数据库中数据的处理转化为对对象的处理.如下图所示: 入门简单实例: hiberante 可以用在 j2se 项目,也可以用在 j2ee (web项目中 ...
随机推荐
- IOS第18天(3,CALayer隐式动画)
******隐式动画(手指拖拽Layer) #import "HMViewController.h" @interface HMViewController () @propert ...
- IOS第15天(2,事件处理hitTest练习)
***hitTest 获取最合适的点 @implementation HMGreenView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEv ...
- Python脚本模拟登录网页之ZiMuZu篇
ZiMuZu.tv这个网站喜欢看电影看美剧的人一定都熟悉. 这个网站原先的升级策略是每天登陆网站, 然后去一个"每日签到"的页面点击一个签到按钮, 以实现帐号等级的升级. 之前网上 ...
- Final-阶段站立会议5
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- LNMP环境简易安装流程
1.关闭防火墙 [root@CentOS ~]# chkconfig iptables off 2.关闭selinux vi /etc/sysconfig/selinux //将SELINUX=enf ...
- SeasLog-An effective,fast,stable log extension for PHP
github: https://github.com/Neeke/SeasLog @author Chitao.Gao [neeke@php.net] @交流群 312910117 简介 为什么使用S ...
- php5-fpm.sock failed (13: Permission denied) error
In order to fix the php5-fpm.sock failed error follow these instructions 1) Make sure your virtual h ...
- cocos2dx 3.x(屏幕截图的两种方法)
[RenderTexture] RenderTexture这个动态纹理类,顾名思义就是可以动态创建纹理图片. 屏幕截图主要步骤: > 开始截图:render->begin(); > ...
- 前端开发利器-Brackets IDE
是什么? http://brackets.io/ A modern, open source text editor that understands web design. 现代, 开源的文本编辑器 ...
- erlang note
没有关于erlang interface ,继续寻找吧... --------------------------------------------------------------- erl - ...