[.NET MVC4 入门系列03]使用Controller访问Model中数据
2017-03-15 15:23
501 查看
这章中学习使用Controller从Model中获取数据,并填充到view template中。
一 、添加MoviesController:
在项目的/Controllers文件夹下新建一个控制器,配置如下图:
*注意:在新建控制器之前需要生成项目,不然找不到我们要用到的模型类(Model class)和数据上下文类(Data Context class)
添加后,会同时生成一个新的/views/Movies文件夹;
在这个文件夹中,会由VS自动添加五个文件,如下图:
[分析]:ASP.NET MVC 4 automatically created the CRUD (create, read, update, and delete) action methods and
views for you (the automatic creation of CRUD action methods and views is known as scaffolding). You now have a fully functional web application that lets you create, list, edit, and delete movie entries.
新建一个带“模板”的Controller时,asp.net MVC4会自动创建一套Action方法CRUD(create,read,update,delete)、以及配套的View。这一套东西直接可以用于操作相关接的 movie Entities 。
二、测试并操作数据:
1.Index视图:
现在就可以测试Movies系列视图了,自动生成的CRUD各对应一个View,再加上Index共5个。
运行后,用http://localhost:54782/Movies (端口号随机的,这不用再解释吧)就可以访问index视图。
注意现在数据库、数据都不存在,我们可以同过Create New来添加。
2. Create视图:
点击Index View中的Create New链接,会跳转到Create View(URL: /movies/create),在其中可以添加记录到数据库中,并在第一次运行时,直接生成一个数据库。
添加完,生成的数据库:Movies.mdf,默认隐藏,需要显示所有文件
添加完后会自动跳转回 Index ,Index View中会自动将新添加的数据显示出来:
点击后面的三个链接,会分别进入Edit View、Details View 和 Delete View。
三、代码分析:
1. MovieDBContext在MoviesController中的应用:
DataBase Context (数据库上下文类)MovieDBContext,是前面我们定义在Movies.cs中,用来操作Movie Entity 的数据库上下问的。
这里创建了他的对象——db,通过db.Movies.ToList()方法作为View方法的参数,将整个Movie数据库中的Movies表内容返回并提交给Index View。
2.System.Data.Entity.DbContext 类
3.System.Data.Entity.DbSet 类
4.强类型Models 和 @model 关键字
到现在,接触到两种在controller和view间传递数据的方式:
1)使用ViewBag类:前面的页面Title处理方法;
2)使用强类型数据或对象:
ASP.NET MVC 允许Controller将强类型的数据或对象传递给View Template;
使用强类型的好处在于:编译时节省检查时间;VS 智能提示更加方便、丰富;
例:Details
先来看movie Controller中action代码
传入参数是电影的id,如果找到,返回一个Movie类对象到视图中。
再来看/views/movies/Details.cshtml
第一行@model MvcApplication1.Models.Movie
通过@model ,可以指派该View所需的对象类型,通过这条代码,就可以在View中随意使用Controller传过来的对应类型的对象了。
在本例中,可以这样理解:View中接收到Movie类对象movie到model中,在上面的cshtml代码中,model指代的就是controller传过来的movie对象。
又如:Index
上面代码中,Controller传给View的就不是单个对象,而是Movie对象列表;
对应的,在/views/movies/index.cshtml 中:
第一行:@model IEnumerable<MvcApplication1.Models.Movie>
model接到的就是一个Movie对象列表了。
一 、添加MoviesController:
在项目的/Controllers文件夹下新建一个控制器,配置如下图:
*注意:在新建控制器之前需要生成项目,不然找不到我们要用到的模型类(Model class)和数据上下文类(Data Context class)
添加后,会同时生成一个新的/views/Movies文件夹;
在这个文件夹中,会由VS自动添加五个文件,如下图:
[分析]:ASP.NET MVC 4 automatically created the CRUD (create, read, update, and delete) action methods and
views for you (the automatic creation of CRUD action methods and views is known as scaffolding). You now have a fully functional web application that lets you create, list, edit, and delete movie entries.
新建一个带“模板”的Controller时,asp.net MVC4会自动创建一套Action方法CRUD(create,read,update,delete)、以及配套的View。这一套东西直接可以用于操作相关接的 movie Entities 。
二、测试并操作数据:
1.Index视图:
现在就可以测试Movies系列视图了,自动生成的CRUD各对应一个View,再加上Index共5个。
运行后,用http://localhost:54782/Movies (端口号随机的,这不用再解释吧)就可以访问index视图。
注意现在数据库、数据都不存在,我们可以同过Create New来添加。
2. Create视图:
点击Index View中的Create New链接,会跳转到Create View(URL: /movies/create),在其中可以添加记录到数据库中,并在第一次运行时,直接生成一个数据库。
添加完,生成的数据库:Movies.mdf,默认隐藏,需要显示所有文件
添加完后会自动跳转回 Index ,Index View中会自动将新添加的数据显示出来:
点击后面的三个链接,会分别进入Edit View、Details View 和 Delete View。
三、代码分析:
1. MovieDBContext在MoviesController中的应用:
1 private MovieDBContext db = new MovieDBContext(); 2 // 3 // GET: /Movies/ 4 5 public ActionResult Index() 6 { 7 return View(db.Movies.ToList()); 8 }
DataBase Context (数据库上下文类)MovieDBContext,是前面我们定义在Movies.cs中,用来操作Movie Entity 的数据库上下问的。
这里创建了他的对象——db,通过db.Movies.ToList()方法作为View方法的参数,将整个Movie数据库中的Movies表内容返回并提交给Index View。
1 public class MovieDBContext : DbContext 2 { 3 public DbSet<Movie> Movies { get; set; } 4 }
2.System.Data.Entity.DbContext 类
3.System.Data.Entity.DbSet 类
4.强类型Models 和 @model 关键字
到现在,接触到两种在controller和view间传递数据的方式:
1)使用ViewBag类:前面的页面Title处理方法;
2)使用强类型数据或对象:
ASP.NET MVC 允许Controller将强类型的数据或对象传递给View Template;
使用强类型的好处在于:编译时节省检查时间;VS 智能提示更加方便、丰富;
例:Details
先来看movie Controller中action代码
1 // GET: /Movies/Details/5 2 3 public ActionResult Details(int id = 0) 4 { 5 Movie movie = db.Movies.Find(id); 6 if (movie == null) 7 { 8 return HttpNotFound(); 9 } 10 return View(movie); 11 }
传入参数是电影的id,如果找到,返回一个Movie类对象到视图中。
再来看/views/movies/Details.cshtml
1 @model MvcApplication1.Models.Movie 2 3 @{ 4 ViewBag.Title = "Details"; 5 } 6 7 <h2>Details</h2> 8 9 <fieldset> 10 <legend>Movie</legend> 11 12 <div class="display-label"> 13 @Html.DisplayNameFor(model => model.Title) 14 </div> 15 <div class="display-field"> 16 @Html.DisplayFor(model => model.Title) 17 </div> 18 19 <div class="display-label"> 20 @Html.DisplayNameFor(model => model.ReleaseDate) 21 </div> 22 <div class="display-field"> 23 @Html.DisplayFor(model => model.ReleaseDate) 24 </div> 25 26 <div class="display-label"> 27 @Html.DisplayNameFor(model => model.Genre) 28 </div> 29 <div class="display-field"> 30 @Html.DisplayFor(model => model.Genre) 31 </div> 32 33 <div class="display-label"> 34 @Html.DisplayNameFor(model => model.Price) 35 </div> 36 <div class="display-field"> 37 @Html.DisplayFor(model => model.Price) 38 </div> 39 </fieldset> 40 <p> 41 @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) | 42 @Html.ActionLink("Back to List", "Index") 43 </p>
第一行@model MvcApplication1.Models.Movie
通过@model ,可以指派该View所需的对象类型,通过这条代码,就可以在View中随意使用Controller传过来的对应类型的对象了。
在本例中,可以这样理解:View中接收到Movie类对象movie到model中,在上面的cshtml代码中,model指代的就是controller传过来的movie对象。
又如:Index
1 // GET: /Movies/ 2 3 public ActionResult Index() 4 { 5 return View(db.Movies.ToList()); 6 }
上面代码中,Controller传给View的就不是单个对象,而是Movie对象列表;
对应的,在/views/movies/index.cshtml 中:
1 @model IEnumerable<MvcApplication1.Models.Movie> 2 3 @{ 4 ViewBag.Title = "Index"; 5 } 6 7 <h2>Index</h2> 8 9 <p> 10 @Html.ActionLink("Create New", "Create") 11 </p> 12 <table> 13 <tr> 14 <th> 15 @Html.DisplayNameFor(model => model.Title) 16 </th> 17 <th> 18 @Html.DisplayNameFor(model => model.ReleaseDate) 19 </th> 20 <th> 21 @Html.DisplayNameFor(model => model.Genre) 22 </th> 23 <th> 24 @Html.DisplayNameFor(model => model.Price) 25 </th> 26 <th></th> 27 </tr> 28 29 @foreach (var item in Model) { 30 <tr> 31 <td> 32 @Html.DisplayFor(modelItem => item.Title) 33 </td> 34 <td> 35 @Html.DisplayFor(modelItem => item.ReleaseDate) 36 </td> 37 <td> 38 @Html.DisplayFor(modelItem => item.Genre) 39 </td> 40 <td> 41 @Html.DisplayFor(modelItem => item.Price) 42 </td> 43 <td> 44 @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | 45 @Html.ActionLink("Details", "Details", new { id=item.ID }) | 46 @Html.ActionLink("Delete", "Delete", new { id=item.ID }) 47 </td> 48 </tr> 49 } 50 51 </table>
第一行:@model IEnumerable<MvcApplication1.Models.Movie>
model接到的就是一个Movie对象列表了。
相关文章推荐
- [.NET MVC4 入门系列03]使用Controller访问Model中数据
- 【翻译】ASP.NET MVC4 入门(五)在Controller中访问Model的数据
- ASP.NET MVC系列:从Controller访问Model数据
- Enterprise Library 4.1数据访问应用程序块快速入门【1】使用DbDataReader检索多行
- Windows Azure入门教学系列 (七):使用REST API访问Storage Service
- [.NET MVC4 入门系列02]MVC Movie 为项目添加Model
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:建立一个EF数据模型
- Windows Azure入门教学系列 (七):使用REST API访问Storage Service
- Enterprise Library 4.1数据访问应用程序块快速入门【2】使用DataSet检索多行
- Enterprise Library Step By Step系列(三):数据访问程序块——入门篇
- Windows Azure入门教学系列 (七):使用REST API访问Storage Service
- [.NET MVC4 入门系列07] 在Model模型模块中添加验证
- Dropthings 2.6 数据访问使用了ado.net data model
- [Silverlight入门系列]使用MVVM模式(5):异步Validation数据验证和INotifyDataErrorInfo接口
- Enterprise Library Step By Step系列(三):数据访问程序块——入门篇
- [Silverlight入门系列]使用MVVM模式(7):ViewModel的INotifyPropertyChanged接口实现
- (LINQ 学习系列)(6)Linq教程实例: 使用自写类代码来访问数据
- [Silverlight入门系列]使用MVVM模式(9): 想在ViewModel中控制Storyboard动画?
- Enterprise Library Step By Step系列(三):数据访问程序块——入门篇
- 使用ClientObjectModel访问SharePoint数据