数迹学——Asp.Net MVC4入门指南(5):从控制器访问数据模型

时间:2022-10-12 05:53:55

MovieController中的方法Index()代码,初认识,应该有很多理解错误的地方,暂时这么记忆吧,待随后修改

Index()代码:

 @model IEnumerable<MVCMovie.Models.Movie>   

 @{
ViewBag.Title = "Index";
Html.EnableClientValidation();
} <p>
@Html.ActionLink("添加电影", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr> @foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("修改", "Edit", new { id=item.ID }) |
@Html.ActionLink("查看详细", "Details", new { id=item.ID }) |
@Html.ActionLink("删除", "Delete", new { id=item.ID })
</td>
</tr>
}</table>

知识点:

(1)@model IEnumerable<MVCMovie.Models.Movie> 

                                           项目名.      空间名.    类名

      IEnumerable<T>接口,该接口支持在指定数据集合上进行迭代操作。它定义了一组扩展方法,对数据集合中的元素进行遍历、过滤、排序、搜索、定位等操作。在lINQ中,数据源实际上实现了这个接口。

      引用model 命名空间MVCMOvie.Models中的类Movie(项目名.空间名.类名)

      简单说就是可以使用类Movie的特性

(2)参看这篇博文 http://www.cnblogs.com/willick/p/3224144.html 

[ASP.NET MVC 小牛之路]03 - Razor语法

   @{

 ViewBag.Title = "Index";
Html.EnableClientValidation();
}

(3)@Html.ActionLink("添加电影", "Create")

(4) @Html.DisplayNameFor(model => model.Title)

(5)@foreach (var item in Model) 

{
  @Html.DisplayFor(modelItem => item.Title)
}
引用的博文说明
MJˊS BLOG

为理想中的状态起航!

MVC HTML辅助类常用方法记录

()@Html.DisplayNameFor(model => model.Title)是显示列名,

()@Html.DisplayFor(modelItem => item.Title)是显示列的内容

()@Html.ActionLink("Create New", "Create")是超链接,跳转到model中的create页面,引用的是controller中create方法;

()@Html.ActionLink("Edit", "Edit", new { id=item.ID })编辑页面;

()@using (Html.BeginForm()) {   @Html.ValidationSummary(true)}用于客户端验证,其Html.BeginForm()表示在本页显示

()<div class="editor-label">
@Html.LabelFor(model => model.Time)标签
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Time)编辑框
@Html.ValidationMessageFor(model => model.Time)验证合法性错误显示
</div> 分类: Asp.net MVC