初尝 MVC4

文章内容参考 http://www.cnblogs.com/leoo2sk/archive/2008/10/27/1320285.html

开发环境 VS2010 ,VS2010 开发 MVC4 需下载 MVC4 安装包,链接见文末。

项目结构:

Content 用来存放图片

Controllers 用来存放控制器类

Models 用来存放 实体以及服务组件

Scripts 用来存放 Javascript 脚本

Views 用来存放界面

先看视图代码

 1 @*注释*@
 2 @using MVC4Demo.Web.Models.Entities;
 3 @using System.Web.Mvc;
 4 @{
 5     Layout = null;
 6 }
 7 <!DOCTYPE html>
 8 <html>
 9 <head>
10     <meta name="viewport" content="width=device-width" />
11     <title>Index</title>
12 </head>
13 <body>
14     @{List<CategoryInfo> categories = ViewData["Categories"] as List<CategoryInfo>;}
15     <div>
16         <h1>
17             MVC公告发布系统</h1>
18         <ul>
19             @{foreach (var c in categories)
20               {
21                   <li>@Html.ActionLink(c.Name, "List/" + c.ID, "Announce")</li>
22               }
23             }
24         </ul>
25     </div>
26 </body>
27 </html>

控制器代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using MVC4Demo.Web.Models.Interfaces;
 7 using MVC4Demo.Web.Models.Entities;
 8 
 9 namespace Mvc4Demo.Web.Controllers
10 {
11     public class HomeController : Controller
12     {
13         //
14         // GET: /Home/
15 
16         public ActionResult Index()
17         {
18             ICategoryService cServ = ServiceBuilder.BuildCategoryService();
19             ViewData["Categories"] = cServ.GetAll();
20 
21             return View("Index");
22         }
23 
24     }
25 }

运行效果:

源代码下载:

http://pan.baidu.com/s/1sj6X8fj

MVC4 for VS 2010 下载地址:

http://pan.baidu.com/s/1sj8t2Zr

原文地址:https://www.cnblogs.com/Aphasia/p/4116800.html