ASP.NET MVC 3.0 Rezor 学习笔记之一

   微软在MVC 3.0 推出了 Razor ,Racor 的使用更为简单。

  1.MVC   执行原理图 。

 接下来用Razor展示一个页面

 

  看Controller 是如何实现的数据传送:

1 public ActionResult Index()
2 {
3 ViewBag.UserList = _usernameinfo;
4 return View();
5 }
6
7 private List<UserNameInfo> _usernameinfo = new List<UserNameInfo>
8 {
9 new UserNameInfo
10 {
11 Name = "jackyong",
12 },
13 new UserNameInfo
14 {
15 Name = "jackpeng",
16
17 }
18 };

 Razor 的展示的View  

@{
ViewBag.Title
= "Home page";
}
@using (Html.BeginForm())
{
<table>
@foreach (var item
in ViewBag.UserList)
{

<tr>
<td>
name
is
</td>
<td>
@item.Name
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Log On" />
</p>
<p>
To learn more about ASP.NET MVC visit
<a href="http://asp.net/mvc" title="ASP.NET MVC Website">
http:
//asp.net/mvc</a>.
</p>


}

  提交将重新返回 Controller,转向LogOn 页面 !

        [HttpPost]
        public ActionResult Index(UserNameInfo model)
        {
            return RedirectToAction("LogOn", "Account");
        }

 网站内实现统一外观的_Layout.cshtml布局文件:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1>
                    My MVC Application</h1>
            </div>
            <div id="logindisplay">
                @Html.Partial("_LogOnPartial")
            </div>
            <div id="menucontainer">
                <ul id="menu">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                </ul>
            </div>
        </div>
        <div id="main">
            @RenderBody()
            <div id="footer">
            </div>
        </div>
    </div>
</body>
</html>

所有视图都默认使用_Layout.cshtml文件的_ViewStart.cshtml文件:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

ASP.NET  MVC 3.0 学习笔记

posted @ 2011-02-23 14:39  jackyong  阅读(1797)  评论(0编辑  收藏  举报