ASP.NET MVC 3 直到我膝盖中了一箭【4】小试牛刀
1.创建一个ASP.NET MVC 3 项目-->选择Razor视图引擎
2.~/Models/下添加类StudentModels-->重新生成解决方案
3.~/Controllers/下添加控制器StudentController-->添加Index视图
4.Global.asax 文件中设置全局 URL 路由默认值 www.2cto.com
1 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // 要忽略的路由的URL模式直接访问.axd文件
2 routes.MapRoute(
3 "Default", // 路由名称
4 "{controller}/{action}/{id}", // 带有参数的 URL
5 new { controller = "Student", action = "Index", id = UrlParameter.Optional } // 参数默认值
6 );
5.未将对象引用设置到对象的实例
1 public ActionResult Index()
2 {
3 return View(GetData());
4 }
5
6 /// <summary>
7 /// 初始化
8 /// </summary>
9 /// <returns></returns>
10 IEnumerable<StudentModels> GetData()
11 {
12 IEnumerable<StudentModels> list = new List<StudentModels>
13 {
14 new StudentModels() {ID = 1001, Name = "张三", Age = 20},
15 new StudentModels() {ID = 1002, Name = "李四", Age = 21},
16 new StudentModels() {ID = 1003, Name = "王五", Age = 22}
17 };
18 return list;
19 }
6.详细信息-->添加Details视图
1 public ActionResult Details(int id)
2 {
3 foreach (var student in GetData())
4 {
5 if (student.ID.Equals(id))
6 {
7 return View(student);
8 }
9 }
10 return View();
11 }
7.创建-->添加Create视图
1 public ActionResult Create()
2 {
3 return View();
4 }
5 [HttpPost]
6 public ActionResult Create(FormCollection collection)
7 {
8 try
9 {
10 return RedirectToAction("Index");
11 }
12 catch
13 {
14 return View();
15 }
16 }
8.更新-->添加Edit视图
1 public ActionResult Edit(int id)
2 {
3 return View();
4 }
5 [HttpPost]
6 public ActionResult Edit(int id, FormCollection collection)
7 {
8 try
9 {
10 return RedirectToAction("Index");
11 }
12 catch
13 {
14 return View();
15 }
16 }
9.删除-->添加Delete视图
1 public ActionResult Delete(int id)
2 {
3 foreach (var student in GetData())
4 {
5 if (student.ID.Equals(id))
6 {
7 return View(student);
8 }
9 }
10 return View();
11 }
12 [HttpPost]
13 public ActionResult Delete(int id, FormCollection collection)
14 {
15 try
16 {
17 // TODO: Add delete logic here
18
19 return RedirectToAction("Index");
20 }
21 catch
22 {
23 return View();
24 }
25 }
10.开始执行
作者 依信依誉
最近更新
随机推荐
- 少年非法入侵网吧主机充值判刑8个月
- Apache严重漏洞安全代码公开
- 萨摩耶金服否认央视3
- 攻击雅虎黑客现身 声称只为找工作
- 外媒评Win 10:看似退让妥协 实则雄心
- 发微博称“炸住建委” 女歌手吴虹飞被
- Mac用户不安宁 1级OSX通过蓝牙传播
- Offensive Security 举办黑客大赛
- 安全代码层出不穷 未修复IE漏洞是最大
- 美国超级黑客网站竟被攻破惨遭涂鸦
- 世界杯指尖上的看球神器 应用汇APP攻
- 移动体制导致创新缺失 自造蛋糕被他人
- ResellerClub新顶级域名研讨会为客户
- Motorola蓝牙接口存在对话框伪造漏洞
- 路由器们的应用平台梦或将破灭!
- 1号店宣布将与京东实现联合采购 扩大
- 多种D-Link路由器UPNP远程栈溢出漏洞
- 快升级 莫让Flash把安全引进来
- 长青洲际渠道总监韩江徽
- 百万美元网络遭入侵 旧金山网络命脉被