您的位置:首页 > 编程语言 > ASP

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

2011-02-23 14:39 134 查看
微软在MVC 3.0 推出了 Razor ,Racor 的使用更为简单。

1.MVC 执行原理图 。



接下来用Razor展示一个页面



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

public ActionResult Index()
{
ViewBag.UserList = _usernameinfo;
return View();
}

private List<UserNameInfo> _usernameinfo = new List<UserNameInfo>
{
new UserNameInfo
{
Name = "jackyong",
},
new UserNameInfo
{
Name = "jackpeng",

}
};


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 学习笔记
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: