欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET MVC 3 直到我膝盖中了一箭【4】小试牛刀

程序员文章站 2022-06-18 09:53:00
  1.创建一个asp.net mvc 3 项目-->选择razor视图引擎 2.~/models/下添加类studentmodels-->重新...

 

1.创建一个asp.net mvc 3 项目-->选择razor视图引擎

2.~/models/下添加类studentmodels-->重新生成解决方案

3.~/controllers/下添加控制器studentcontroller-->添加index视图

ASP.NET MVC 3 直到我膝盖中了一箭【4】小试牛刀

4.global.asax 文件中设置全局 url 路由默认值


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.开始执行

ASP.NET MVC 3 直到我膝盖中了一箭【4】小试牛刀

 

 


作者 依信依誉