ASP.NET MVC 3 学习笔记系列之Music Store(2)

本篇主要是介绍了如何通过Model来进行数据的展示。

 

通过模型传递信息给View

Contorllers 的Action 方法返回的ActionResult可以将Model对象传递给View。

在models 文件夹上通过“Add Class” 方法添加 Genres 和Albums  的Model Classses;

修改StoreController 类, 增加Details 的Action

image

创建Details的View:

image

生成的发Details.cshtml代码如下:

image

@model MvcMusicStore.Models.Album 表示这个View绑定了强类型MvcMusicStore.Models.Album

@Model.Title 表示从Model中获取Title 属性值。

修改StoreController的Index 方法如下:

image

创建Index.cshtml View

image

修改: @model MvcMusicStore.Models.Genre  为 @model IEnumerable<MvcMusicStore.Models.Genre>,表明这个View绑定的类型为Genre的集合。

页面之间增加链接

修改Index.cshtml 文件, 代码如下:

image

@Html.ActionLink 的三个参数

1)链接的现实名称

2)Contoller 的Action名称

3) 参数, 同时需要指定参数名称和值

原文地址:https://www.cnblogs.com/warren/p/2184866.html